如何在criteria中使用'in'查询子对象?
How do I use 'in' in criteria to query sub-objects?
我想查询给定列表中所有具有子对象的对象。
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
除了@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)
}
}
大同小异,只是写法不同罢了。
我想查询给定列表中所有具有子对象的对象。
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
?
除了@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)
}
}
大同小异,只是写法不同罢了。