I have two domains
class DomainA {
String name
Date dateCreated
Date lastUpdated
static transients = ['email']
static hasMany = [domainBs: DomainB]
public String getEmail() {
DomainB.mostRecentRecord(this).get()?.email
}
}
and
class DomainB {
String email
Date dateCreated
Date lastUpdated
static belongsTo = [domainA: DomainA]
static namedQueries = {
mostRecentRecord { domainA ->
eq 'domainA', domainA
order('dateCreated', 'desc')
maxResults(1)
}
}
}
My requirement is to get list of all DomainA whose name starts with "M" and latest domainBs record contains gmail in their email property.
I tried createCriteria
and hql
but did not get desired result, may be I am doing something wrong.
Following is my current code
List<DomainA> listA = DomainA.findAllByNameIlike("M%")
List<DomainB> listB = []
listA.each { entity ->
DomainB domainB = DomainB.mostRecentRecord(entity).get()
if (domainB && (domainB.email.contains('gmail'))) {
listB.add(domainB)
}
}
but it does not allows pagination and sort.
Can someone have any idea to get list of all DomainA whose name starts with "M" and latest domainBs contains gmail in their email property using createCriteria
or hql
or any other way.