Saturday, January 19, 2013

How to get rid of lazy collection initialization in Hibernatee4

Is there anybody out there who never hit the error
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: [...], no session or session was closed?

Well, now with Hibernate4 (starting from version 4.1.6.Final) the troubles are gone, hopefully once for all!

The trick is very simple. Just locate your persistence.xml file and add

<property name="hibernate.enable_lazy_load_no_trans" value="true"/>

under the "properties" node.