How can I read only the lastChild along with parentObject
Parent {
hasMany[children: Child]
}
here is my code
Parent.withCriteria() {
eq("active", true)
children {
order("dateCreated", "desc")
maxResults(1)
}
}
But not working. How can I read Parent with last updated child
hasMany
in GORM/Hibernate does NOT have a filter semantics and always returns all referenced objects.
In a straight-forwad case you have to fire 2 queries: to find all Parents
and to find all last updated children.
Another option would be to reverse the search logic: look for children 1st, group them by lastUpdated
and then pull their parents. Of course, you must have a back-ref from Child to Parent:
class Child {
static belongsTo = [ parent:Parent ]
}
Then the query could look something like:
Child.withCriteria{
projections{
groupProperty 'id'
max 'lastUpdated'
}
parent{
eq "active", true
}
}