Groovy版本2.4.8 Grails版本2.5.1
我正在尝试使用like子句从我的Advisor表中拉出行,而且如果有公司名称传递到该方法中,那么我只想从该公司拉出顾问。
我构造了两个查询,其中一个没有固定组件,可以很好地工作,但当我取消注释设置公司的行以测试第二个查询运行时,我得到以下异常
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
}发布于 2017-02-25 01:57:01
应在同一贴图中设置这两个参数,如下所示:
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]) https://stackoverflow.com/questions/42405519
复制相似问题