我想查询给定列表中包含子对象的所有对象。
class Parent {
Child child
static namedQueries = {
getParentsByListOfChildren { childList ->
'in'('child', childList.collect {it.id})
}
}
}
class Child {
}
def listOfChildren = [child1, child2] // child1 and child2 are instances of Child
def results = Parent.getParentsByListOfChildren(listOfChildren)
我在集成测试中遇到以下异常:
property.BasicPropertyAccessor HHH000122: IllegalArgumentException in class: com.me.Parent, getter method of property: id
| Failure: getParentsByListOfChildren correctly filters trains (com.me.ParentIntegrationSpec)
| org.hibernate.PropertyAccessException: IllegalArgumentException occurred calling getter of com.me.parent.id
at org.grails.datastore.gorm.GormStaticApi.withCriteria_closure11(GormStaticApi.groovy:304)
at org.grails.datastore.mapping.core.DatastoreUtils.execute(DatastoreUtils.java:302)
at org.grails.datastore.gorm.AbstractDatastoreApi.execute(AbstractDatastoreApi.groovy:37)
at org.grails.datastore.gorm.GormStaticApi.withCriteria(GormStaticApi.groovy:303)
如何在对象不是简单对象的条件中使用in
?我假设我需要使用Child
的ID?
答案 0 :(得分:1)
除了@dmahapatro建议的方法外,还有其他两种方法可以编写条件查询。我指的是他们在这里供参考。
解决方案1 :@dmahapatro建议
getParentsByListOfChildren { childList ->
'in'('child.id', childList*.id )
}
解决方案2 :
getParentsByListOfChildren { childList ->
createAlias("child", "childFoo") // Name is just an example, you can use anything in second parameter
'in'('childFoo.id', childList*.id)
}
解决方案3 :
getParentsByListOfChildren { childList ->
child {
'in'('id', childList*.id)
}
}
一切都很相似,只是根据方便,写作风格不同。