bsr
bsr

Reputation: 58662

Lazy fetching of objects using FindAllBy , for the first time

When I use criteria queries, the result contains array list of lazy initialized objects. that is, the list has values with handler org.codehaus.groovy.grails.orm.hibernate.proxy.GroovyAwareJavassistLazyInitializer.

This prevent me from doing any array operation (minus, remove etc) in it. When I use, GORM methods, I get array list of actual object types. How can I get the actual objects in criteria query?

  1. The code is listed below.
availableTypes = Type.withCriteria() {   
    'in'("roleFrom", from) 
    'in'("roleTo", to) 
}

availableTypes (an array list) has one value , but not actual object but value with a handler of GroovyAwareJavassistLazyInitializer

  1. availableTypes (an array list) has values with type Type
availableTypes = Type.findByRoleFrom(from)

---------- Update ----------
I did further troubleshooting, and this is what I found. Probably the above description might be misleading, but I kept it in case it helps.

def typeFrom = Type.findAllByParty(partyFrom)
def relFrom = Relation.findAllByParty(partyFrom)  
class Role {
      RoleType roleType  
      LocalDate validFrom  
      LocalDate validTo      
      static belongsTo = [party : Party ]  
    ...  
}

I know if I do statement like Party.findAll(), the role instances would be proxy till they access. But, when using gorm directly on the class (Role), why I am getting the proxy objects ???

thanks for the help.

thanks.

Upvotes: 2

Views: 2571

Answers (1)

msanjay
msanjay

Reputation: 2343

Turns out are a couple of possible solutions which I came across but didn't try, such as

  • Overloading the equals method so that the proxy and the domain object use a primary key instead of the hashCode for equality
  • Using a join query so that you get actual instances back and not proxies
  • GrailsHibernateUtil.unwrapProxy(o)
  • HibernateProxyHelper.getClassWithoutInitializingProxy(object)

One solution that worked for me was to specify lazy loading to be false in the domain object mapping.

History of this problem seems to be discussed here: GRAILS-4614

See also: eager load

Upvotes: 1

Related Questions