Grails 使用findAllBy方法访问对象属性列表时出现的问题
下面是获取由params生成并从控制器传递到服务中的标记列表的代码。我正在尝试筛选餐馆列表,以仅包含具有匹配标记属性的餐馆。我不熟悉编程和grails,所以请原谅我犯的任何愚蠢的错误:Grails 使用findAllBy方法访问对象属性列表时出现的问题,grails,gorm,Grails,Gorm,下面是获取由params生成并从控制器传递到服务中的标记列表的代码。我正在尝试筛选餐馆列表,以仅包含具有匹配标记属性的餐馆。我不熟悉编程和grails,所以请原谅我犯的任何愚蠢的错误: class Eatery { String name String phone Date lastVisited List<Tag> tags static hasOne = [location: Location
class Eatery {
String name
String phone
Date lastVisited
List<Tag> tags
static hasOne = [location: Location]
static hasMany = [tags: Tag];
static constraints = {
name nullable: false, blank: false
phone nullable: true, blank: true
location nullable: true, blank: true
tags nullable: true, blank: true
lastVisited nullable: true, blank: true
}
public String toString() {
return name
}
}
以下是服务方法:
Eatery getRandomEatery(List<Tag> tagList) {
List<Eatery> eateryList = Eatery.findAllByTags(tagList)
Random random = new Random()
Integer n = eateryList.size()
Integer selection = Math.abs(random.nextInt(n))
Eatery eatery = eateryList.get(selection)
return eatery
}
下面是错误:
Class
org.h2.jdbc.JdbcSQLException
Message
Parameter "#1" is not set; SQL statement: select this_.id as id2_0_, this_.version as version2_0_, this_.last_visited as last3_2_0_, this_.name as name2_0_, this_.phone as phone2_0_ from eatery this_ where this_.id=? [90012-164]
Around line 16 of grails-app/services/grubspot/RandomizerService.groovy
13://14:// }15: List<Eatery> eateryList = Eatery.findAllByTags(tagList)16: Random random = new Random()17: Integer n = eateryList.size()18: Integer selection = Math.abs(random.nextInt(n))19: Eatery eatery = eateryList.get(selection)
改变
到
我想你可以用一个类似于
List<Eatery> eateryList = Eatery.withCriteria{
tags {
"in" "id", tagList.id
}
}
还没有测试过,但应该给你一个想法
List<Eatery eateryList = Eatery.executeQuery("""
select e
from Eatery e left join e.tags as t
where t in (:tagList)""", [tagList: tagList])
List<Eatery> eateryList = Eatery.withCriteria{
tags {
"in" "id", tagList.id
}
}