Groovy版本2.4.8 Grails版本2.5.1
我试图使用like子句从我的Advisor表中提取行,如果有一个公司名称传入方法,那么我只想从该公司拉出顾问。
我构建了两个没有Firm组件的查询,但是当我取消注释设置公司的行以测试第二个查询运行时,我得到了以下异常
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
}
答案 0 :(得分:0)
你应该在同一张地图中设置两个参数,如下所示:
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])