-->
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.  [ 2 posts ] 
Author Message
 Post subject: Why should get() return a proxy to a loaded object?
PostPosted: Thu Aug 07, 2008 6:13 am 
Newbie

Joined: Wed Sep 12, 2007 1:58 pm
Posts: 12
Hi guys,

I was debugging some code and noticed that calling session.get() returns a proxy if the proxy exists in the persistence context. This came as a bit of a surprise, especially after reading HiA (that's addressed here http://forum.hibernate.org/viewtopic.php?t=941435).

While trying to understand what was going on, I traced through the Hibernate code and found that a database hit was actually incurred, and the loaded entity set to the implementation field of the lazy initializer of the proxy.

So here's what I can't figure out. If the database hit is incurred anyway, and if the object is loaded, why doesn't get() immediately return the entity instead of a proxy to it? Is there any reason not to?

Thanks,
mers



Hibernate version:
3.2.6

Code:
sessionFactory.getCurrentSession().load(Car.class, 1);
sessionFactory.getCurrentSession().get(Car.class, 1);


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 07, 2008 6:39 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
My theory is that this behavior is due to that in the scope of a session Hibernate guarantees that a given entity in the database is always represented by the same object. So, if a proxy already exists in the session, then this proxy must always be returned in all methods otherwise you will have two different objects representing the same entity.

The database is probably hit because the proxy hasn't been initialized and Hibernate need to check that it actually exists in the database, since it has to return null if it don't.

Here is some more information about object identity:
http://www.hibernate.org/hib_docs/v3/re ... s-identity


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