1

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']])
Anonymous1
  • 3,877
  • 3
  • 28
  • 42
  • do you use everything from propertyToJoinSometimes or are you then using bits from owner and bits from propertyToJoinSometimes to make up your presentation ? Reason I ask - you can refer to an earlier answer here http://stackoverflow.com/questions/38363404/grails-3-return-list-in-query-result-from-hql-query/38393443#38393443 – V H Sep 23 '16 at 20:22
  • haha haha it was you who had asked the question – V H Sep 23 '16 at 20:28

2 Answers2

0

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)
}
tylerwal
  • 1,858
  • 2
  • 15
  • 20
0

Just to add an option using dynamic finder:

Owner.findAllByOwnerType(2, [fetch: ['propertyToJoinSometimes': 'eager']])
Elias Medeiros
  • 395
  • 3
  • 10