Friday, August 20, 2010

Lazy Initialization Error

Beware of passing domain objects around. If you pass a domain object to other functions, and then try and traverse the associations (many-many, one-one) sometimes you can get into a situation where you get a Lazy Initialization error. This is because Domain objects are by default loaded lazily (more info).

You can get around this
  • By actually reloading the domain object in each function, although this may seem slow, hibernate will cache these calls (if you allow it)
  • You can turn the associations in the Domain object to fetch eagerly (though this may have a performance hit, as more of the object is loaded)
  • You can use a constraints() load to specifically eager fetch you required relationships (see Querying with eager fetching)

No comments:

Post a Comment