我如何只将lastChild和parentObject一起阅读
Parent {
hasMany[children: Child]
}
这是我的代码
Parent.withCriteria() {
eq("active", true)
children {
order("dateCreated", "desc")
maxResults(1)
}
}
但不起作用。我如何阅读最近更新的孩子的父母
GORM / Hibernate中的[hasMany
没有过滤语义,并且始终返回所有引用的对象。
在简单的情况下,您必须引发2个查询:查找所有Parents
和查找所有最近更新的子代。
[另一种选择是颠倒搜索逻辑:首先寻找孩子,将他们按lastUpdated
分组,然后拉他们的父母。当然,您必须从子代到父代都有反向引用:
class Child {
static belongsTo = [ parent:Parent ]
}
然后查询可能类似于:
Child.withCriteria{
projections{
groupProperty 'id'
max 'lastUpdated'
}
parent{
eq "active", true
}
}