我知道有关于这个问题的几个问题,但没有人似乎为我工作。我有一个Grails应用程序有以下域对象:的Grails:查询的hasMany关联
class Tag {
String name
}
class SystemTag extends Tag {
// Will have additional properties here...just placeholder for now
}
class Location {
String name
Set<Tag> tags = []
static hasMany = [tags: Tag]
}
我想查询已经标记由1个或多个标签的所有区域对象:
class LocationQueryTests {
@Test
public void testTagsQuery() {
def tag = new SystemTag(name: "My Locations").save(failOnError: true)
def locationNames = ["L1","L2","L3","L4","L5"]
def locations = []
locationNames.each {
locations << new Location(name: it).save(failOnError: true)
}
(2..4).each {
locations[it].tags << tag
locations[it].save(failOnError: true)
}
def results = Location.withCriteria {
tags {
'in'('name', [tag.name])
}
}
assertEquals(3, results.size()) // Returning 0 results
}
}
我已经验证了数据正在创建/设置正确... 5创建位置对象,并标记最后3个对象。
我看不出有什么不对上面的查询。我真的希望远离HQL,我相信在这里应该是可能的。
'Location.count()'返回5?您可能需要为保存呼叫添加'flush:true'。 – doelleri