I'm having problems with detached objects and LazyInitializationException on a Set with Hibernate 3. After having read through Hibernate in Action and tried every possibly combination of metadata, changing hibernate.max_fetch_depth, adding Hibernate.initialize( ) before closing the session etc, I started searching the web for more information on working with detached objects.
On the web, I found several suggestions for workarounds: like saving the session for the whole web request or using the Spring OpenSessionInViewFilter, but not much on how lazy loading works. I found at least two blogs that suggested not closing the session for the duration of a request, just to avoid getting a LazyInitializationException.
Does anybody know of a good explanation of how to get an object graph loaded (other than the HIA book)?
Here is the code that is giving me a headache, with class names changed to the HIA book example:
Code:
<class name="Item" table="items">
...
<set name="bids" inverse="true" lazy="false" >
<key column="ITEM_ID" />
<one-to-many class="Bid" />
</set>
.... a bunch of properties...
</class>
<class name="Bid" table="bids">
...
<many-to-one name="item" column="ITEM_ID" class="Item" cascade="save-update"/>
.... a bunch of properties, sets and many-to-one mappings ...
</class>
The following code gives a LazyInitializationException:
Code:
Session session = sessionFactory.openSession();
Item item = (Item) session.get(Item.class, id);
Hibernate.initialize(item.getBids());
session.close();
item.getBids(); --> throws LazyInitializationException
I have tried with criteria.setFetchMode("bids", FetchMode.EAGER), without success.
Jonas