-->
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: strange loading of entity that doesn't exists
PostPosted: Fri Apr 07, 2006 8:39 am 
Newbie

Joined: Tue May 25, 2004 11:20 am
Posts: 17
Location: Munich, Germany
I'm facing a very strange behavior of session.load(MyClass.class, <primaryKey>). Hibernate give me back an object (MyClass$$EnhancerByCGLIB$$...) even if the row with primaryKey doesn't exist in the database table. I expected an Exception instead of these proxy object.

Changing the code to session.get(...) than the result is null.

I'm using hibernate-3.2cr1 with hibernate-annotations-3.1beta9 (build the day before yesterday from SVN).

Any help is welcome.

Regards
-chris


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 21, 2006 12:39 pm 
Beginner
Beginner

Joined: Tue Dec 09, 2003 3:27 pm
Posts: 21
I'm getting this for known pk's as well. I'm not sure if I'm missing an annotation or something as the old mapping files will return a correct object. Is this a bug or something I'm missing on my part?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 21, 2006 1:46 pm 
Senior
Senior

Joined: Tue Mar 09, 2004 2:38 pm
Posts: 141
Location: Lowell, MA USA
You're getting back a proxy which represents the class your after. Hibernate uses CGLIB to generate proxies at startup.

Ryan-

_________________
Ryan J. McDonough
http://damnhandy.com

Please remember to rate!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 21, 2006 2:13 pm 
Beginner
Beginner

Joined: Tue Dec 09, 2003 3:27 pm
Posts: 21
ok yeah I figurred my old mappings were using proxies as well but it appears that maybe they weren't.

The reason I thought something was wrong is that the proxies don't show up in our IDE's debugger correctly (every property shows up as null, I see now that the getter methods still return the proper values) however if you use the session.get method instead it appears as though it doesn't use a proxy, hence why I thought there was a problem with the session.load method with annotations.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 21, 2006 3:48 pm 
Beginner
Beginner

Joined: Tue Dec 09, 2003 3:27 pm
Posts: 21
ok so doing a @Proxy(lazy=false) annotation will turn off the proxy. Not sure why we had our old mapping files with lazy="false" to begin with.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 21, 2006 7:20 pm 
Newbie

Joined: Tue May 25, 2004 11:20 am
Posts: 17
Location: Munich, Germany
after all it seems to me that it's just a bug in H3 (with annotations). It doesn't make sense to get a proxy of a non-existing object, does it?

-chris


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 21, 2006 7:25 pm 
Beginner
Beginner

Joined: Tue Dec 09, 2003 3:27 pm
Posts: 21
yes I would say it would be, I think it's also strange that it wouldn't create a proxy using a session.get or a fetching objects using a criteria.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Apr 23, 2006 6:20 am 
Newbie

Joined: Tue Nov 15, 2005 4:21 am
Posts: 11
It does make sense to have a proxy to an nonexistant object. From the docs (emphasis mine):

Quote:
Note that load() will throw an unrecoverable exception if there is no matching database row. If the class is mapped with a proxy, load() just returns an uninitialized proxy and does not actually hit the database until you invoke a method of the proxy. This behaviour is very useful if you wish to create an association to an object without actually loading it from the database.


There's no way it can throw an exception for nonexistance before hitting the database. load() should only be used if you are sure that the object exists.


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.