-->
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.  [ 4 posts ] 
Author Message
 Post subject: Session.load() issue.
PostPosted: Mon Nov 17, 2003 6:06 pm 
Beginner
Beginner

Joined: Fri Sep 12, 2003 12:54 pm
Posts: 20
Hi all,

I've got a Session.load(..) issue I'd like some pointers with...

I'm using hibernate Sessions across web-requests, as described in the refrence docs section 14.3.1: "Long session with automatic versioning"

I am purposely holding references to some objects that I load, and dropping references to others... (depending on whether I need to cache them or not etc.)

Because of the structure of the various code components that a single request thread goes through, it is sometimes possible for a load() to be called for a given entity Id more than once for the session. This results in the following type of exception:

Code:
net.sf.hibernate.HibernateException: The object with that id was already loaded by the Session: [com.foo.Foo#297e80a3f8eb5933dd33f333433ffdcd3]


My questions are:

1- Why can't Hibernate just go ahead an return the instance that it did before?

2- If the Hibernate Session has a handle to the instance it previously load()'ed, is there a way I can get a hold of it from Hibernate since #1 doesn't happen? -- I notice there are contains() and evict() methods on the session, but not any accessor that I can find.

3- Any suggested work-arounds? (For various reasons, I can't hold onto the previously loaded object and pass it around between components - for instance because component A that does the first load has no idea that component B will be doing a second load of the same object)


Also (An unrelated question): if I use update() on a object (that contains Lists etc.) to 'reconnecti' it to a new session, and the object has a "version" property for optimistic locking, does the update short circuit as soon as it detects the version # is the same?


thanks,
james


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 17, 2003 7:27 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
You are mistaken. load() of course returns the entity currently associated with the session.


Top
 Profile  
 
 Post subject: Re: Session.load() issue.
PostPosted: Mon Nov 17, 2003 7:37 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
jhouse wrote:
1- Why can't Hibernate just go ahead an return the instance that it did before?

Because you asked it to fill an already existing instance, not a new one.

jhouse wrote:
2- If the Hibernate Session has a handle to the instance it previously load()'ed, is there a way I can get a hold of it from Hibernate since #1 doesn't happen? -- I notice there are contains() and evict() methods on the session, but not any accessor that I can find.

Use Object session.load(Class, Serializable) instead of void session.load(Object, Serializable)

jhouse wrote:
Also (An unrelated question): if I use update() on a object (that contains Lists etc.) to 'reconnecti' it to a new session, and the object has a "version" property for optimistic locking, does the update short circuit as soon as it detects the version # is the same?

Since version numbers are the same, hibernate will actually do the update.
If version number are differents, another session had already update it and the second update will fail.

_________________
Emmanuel


Top
 Profile  
 
 Post subject: Thanks!
PostPosted: Mon Nov 17, 2003 7:42 pm 
Beginner
Beginner

Joined: Fri Sep 12, 2003 12:54 pm
Posts: 20
Thank you Gavin and Emmanuel!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.