I have a domain class:
class Owner {
Integer ownerType
Prop propertyToJoinSometimes
static constraints = {
propertyToJoinSometimes nullable: true
}
}
I usually don't want to load propertyToJoinSometimes when loading Owner, but I sometimes load many Owner objects at once using findAllBy, and a join could save a large number of calls to the database. Is there a way to do something like:
Owner.findAllByOwnerType(2, [propertyToJoinSometimes: [fetch: 'join']])
This is not exactly what you are looking for (not using the dynamic finder findAllBy
) but it produces the results you are after. The grails documentation for createCriteria/withCriteria does not mention it but there is a fetchMode
method within the HibernateCriteriaBuilder.
import org.hibernate.FetchMode
Owner.withCriteria {
eq('ownerType', 2)
fetchMode('propertyToJoinSometimes', FetchMode.JOIN)
}