-->
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.  [ 8 posts ] 
Author Message
 Post subject: Multiple copies of an object in a session
PostPosted: Wed Dec 10, 2003 7:19 pm 
Regular
Regular

Joined: Tue Sep 02, 2003 5:09 pm
Posts: 81
Location: Whitefish Montana
How do you retrieve an entity object in a session without getting a new copy? I have a developer that is doing a session.load() for an object and then an update. The update is saying the object's Id is a duplicate of an existing object in the session.

I walked through his code and it sure appeared that he is retrieving the list in one session and doing the load in another but that must not be the case. I think there are some design issues in re-retrieving the object but I wonder how would you reload an object -- let's say you had two classes working against a common database?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 10, 2003 8:06 pm 
Expert
Expert

Joined: Tue Sep 16, 2003 4:06 pm
Posts: 318
Location: St. Petersburg, Russia
I'm not sure I understand your question :)
But if it is about reloading already loaded object from the database - session.refresh() will help.


Top
 Profile  
 
 Post subject: Re: Multiple copies of an object in a session
PostPosted: Thu Dec 11, 2003 5:59 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
dmmorris wrote:
How do you retrieve an entity object in a session without getting a new copy?

You don't have a copy.
If you did session.load() then the object is already linked to the session. Then it don't have to be session.update

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 11, 2003 6:31 am 
Expert
Expert

Joined: Fri Nov 07, 2003 4:24 am
Posts: 315
Location: Cape Town, South Africa
Code:
Object o = new Object();
session.load(Object, Serializable)

loads the object with the given identifier into the given transient instance. You run the risk of having multiple instances of the Object for a particular session.

Code:
Object o = session.load(Class, Serializable)

returns the persistant object (possibly from the cache) with the given identifier.

Justin


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 11, 2003 2:41 pm 
Regular
Regular

Joined: Tue Sep 02, 2003 5:09 pm
Posts: 81
Location: Whitefish Montana
I built a test that duplicates his problem. He has several classes that reference each other. Class A is referenced indirectly by Class B via Class C. He is running:

load A
make changes to A
hql query C -- this also retrieves a copy of B which gets a copy of A.
if C is OK then update A

He tells me that it is a problem to move load A/change A after the query to C

I will see what it will take to restructure the code since there doesn't appear to be any way to override this behavior.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 12, 2003 6:56 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Actually Hibernate will flush the session before the HQL of C since C is kind of related to A. So A is already updated before your HQL.

No particular pb to load A after reading C

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 12, 2003 3:02 pm 
Regular
Regular

Joined: Tue Sep 02, 2003 5:09 pm
Posts: 81
Location: Whitefish Montana
I agree that is what I expected but here is the actual code:

Code:
         // Create Another Session
            ThreadLocalSession.setSession(new ServiceSession());

            // Update Record           
            ThreadLocalSession.getSession()
                              .beginTransaction();
                             

         adjustmentApprovalService.load(adjustmentApproval.getAdjustmentApprovalId());
         CashReceiptsDetail crDtl = (CashReceiptsDetail) crdService.load(crdId);            adjustmentApproval.setAdjustmentAmount(999.99);
            adjustmentApprovalService.update(adjustmentApproval);
            ThreadLocalSession.getSession()
                              .commit();

            // Close Session
            ThreadLocalSession.getSession()
                              .close();


This code fails on the update because the crDtl object causes the adjustmentApproval object to be read. I fixed it by using saveOrUpdateCopy.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Dec 13, 2003 6:09 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
I told you no particular pb to load A after reading C, I did not say no particular pb to update a transiant A after reading the persistent A

_________________
Emmanuel


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

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.