I have two entities joined by a join table. When I try to fetch all the associated entities, this only fetches the identity of the associations, not their property values:
User.createCriteria().get {
eq property, value
fetchMode 'authorities', FetchMode.JOIN
}
results only in a join with the join table:
...
left outer join
search_role_auth_user authoritie2_
on this_.ID=authoritie2_.AUTHORITIES_ID
...
How can I eagerly fetch the the associated entity's data as well?
Assuming,
class User{
static hasMany = [authorities: Authority]
}
class Authority{
static belongsTo = [user: User]
}
authorities
can be eagerly fetched in criteria as below
User.createCriteria().list {
eq property, value
authorities{
//All authorities for User are eagerly fetched by default
}
}
Note:-
list
is used instead of get
because there is a possibility of getting more than one result as the criteria is built based on an id
comparison but a property value.