【发布时间】:2017-02-23 01:39:25
【问题描述】:
Groovy 版本 2.4.8 Grails 2.5.1 版
我正在尝试使用 like 子句从我的顾问表中提取行,并且如果有一个公司名称传递到方法中,那么我只想从该公司中提取顾问。
我构建了两个查询,其中一个没有 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
}
【问题讨论】:
-
我没用过这个语法,但是你试过在同一张地图中设置两个参数吗?像这样: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 + ' %', 公司:公司], [max:maxResults])
-
@Eylen 完美!不知道为什么我为每个参数决定了一个地图,可能只是在示例和模式匹配中看到了一个新的 max 地图。谢谢!!
标签: postgresql grails grails-orm