的Groovy版本2.4.8 的Grails版本2.5.1麻烦与GORM查询与LIKE子句和外键/属于-条款
我试图用类似条款从我的顾问表拉行,也如果有传入该方法的公司名称,那么我只想从该公司取得顾问。
我构建无工作正常的企业组成的两个查询一个,但是当我取消那台公司为了测试运行第二个查询行,我得到了下面的异常
org.springframework.orm.hibernate4.HibernateQueryException: Not all named parameters have been set: [firm] [from Advisor a where lower(a.firstName) like:keystrokes OR lower(a.lastName) like:keystrokes AND a.firm.name = :firm];
代码:
def getAdvisorsForKeystrokes(String keystrokes, String firm, int maxResults) {
List<Advisor> advisors;
firm = "Test Firm Name"
if(firm.allWhitespace) {
advisors = Advisor.findAll('from Advisor a where lower(a.firstName) like:keystrokes OR lower(a.lastName) like:keystrokes', [keystrokes:keystrokes + '%'], [max:maxResults])
} else {
advisors = Advisor.findAll('from Advisor a where lower(a.firstName) like:keystrokes OR lower(a.lastName) like:keystrokes AND a.firm.name = :firm', [keystrokes:keystrokes + '%'], [firm:firm], [max:maxResults])
}
return advisors
}
类:
class Advisor {
String firstName
String lastName
String fullName
String city
String state
Firm firm
static belongsTo = [Case, Firm]
static hasMany = [cases:Case]
static constraints = {
}
}
class Firm {
String name
static constraints = {
}
}
如果任何人有任何问题或是一个很好的解决方案,这将是惊人的,谢谢!
编辑:
我知道它可以重写像下面和工作,但我尝试了许多不同的方法来做到这一点在一个查询,这是麻烦的是我一直没能找到一种方式来获得它的工作。
def getAdvisorsForKeystrokes(String keystrokes, String firm, int maxResults) {
List<Advisor> advisors;
advisors = Advisor.findAll('from Advisor a where lower(a.firstName) like:keystrokes OR lower(a.lastName) like:keystrokes', [keystrokes:keystrokes + '%'], [max:maxResults])
if(!firm.allWhitespace) {
def firmModel = Firm.findByName(firm)
advisors = advisors.findAll{ adv ->
adv.firm == firmModel
}
}
return advisors
}
我没有使用过这种语法,但你有没有试过在同一地图设置这两个PARAMS?像这样:advisors = Advisor.findAll('from Advisor a where lower(a.firstName)like:keystrokes or lower(a.lastName)like:keystrokes AND a.firm.name =:firm',[keystrokes:keystrokes +' %',firm:firm],[max:maxResults]) – Eylen
@Eylen完美!不知道为什么我为每个参数决定了一张地图,可能只是在示例和模式匹配中看到了一个新的max地图。谢谢!! – NewDev