-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Lazy Loading 1:n associations makes Hibernate session dirty
PostPosted: Thu Jan 03, 2008 6:48 pm 
Newbie

Joined: Tue Mar 27, 2007 2:00 pm
Posts: 4
Lazy Loading 1:n associations makes Hibernate session dirty when associations fetched in different transactions


We are developing a J2EE application that uses SEAM 2.0.1 CR1 with JSF 1.2 and EJB 3.0/JPA using Hibernate as the JPA provider. All of the persistent entities have dynamicUpate=true so we update only the modified columns.

In one of the pages in the J2EE application, the user can click an entry in the search results page to load the corresponding domain object (called root domain object) and show its details on a details page. The details page contains multiple tabs and clicking a particular tab should cause the backend to load the corresponding 1:n association and associate the collection with the root domain object. The association objects contain the data to be used to render the display in the selected tab. The user can then edit details in the selected tab view and then, hit Submit to save the changes to the database. If the user has made some changes on the screen, the application will first prompt the user if they want to save their changes or not. If they do, then the changes are saved to the database using dynamic update feature (only update the changed columns).

The problem we are facing is: when we lazily load associations (at the time the user clicks the correponding tab) and associate them with the root domain object, it marks the Hibernate session as dirty contrary to our expectations. This is because we are simply fetching these associations from the database and hence, these associations should continue to keep the Hibernate session in un-dirty state (dirty = false). Only when the user performs some edits in the tab view, hibernate session should be set to dirty.

Here are the skeleton definitions of our entities:

//Entity for the selected entry
@javax.persistence.Entity
@org.hibernate.Entity(dynamicUpdate=true)
public class Root
{
@OneToMany(fetch=FetchType.EAGER, cascade={CascadeType.MERGE, CascadeType.REFRESH})
private List<SubRoot> subRoots;

}

//SubRoot is defined as:
@javax.persistence.Entity
@org.hibernate.Entity(dynamicUpdate=true)
public class SubRoot
{
@OneToMany(fetch=FetchType.LAZY, cascade={CascadeType.MERGE, CascadeType.REFRESH})
private List<SubRootDetail> subRootDetails;

}


//SubRootDetails is defined as:
@javax.persistence.Entity
@org.hibernate.Entity(dynamicUpdate=true)
public class SubRoot
{
private BigDecimal detail1;
private BigDecimal detail2;

}

Root domain object is loaded when the user selects an entry in the search results page.
When the Root domain object is loaded, SubRoot objects are also loaded using an EAGER fetch.
The SubRootDetails objects are not loaded until the user clicks the SubRootDetails tab.
When the user clicks the SubRootDetails tab, the SubRootDetails objects are loaded and the session is marked dirty right after the statement: subRoot.setSubRootDetails(subRootDetailsCollectionFromDB).


As a work-around, we are having to load the SubRootDetails objects in the same transaction where we load the Root and its SubRoot objects. I wonder if Hibernate is making the session dirty because we are loading the SubRootDetails objects in a different transaction than where we loaded the Root and SubRoot objects in.

Note: We are not using EJB Transactions in this case. We are using Seam-managed transactions and Seam managed persistence contexts.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.