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