-->
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: REALLY lazy loading
PostPosted: Wed Feb 25, 2009 12:27 am 
Newbie

Joined: Wed Feb 25, 2009 12:01 am
Posts: 2
Hibernate version: 3.3.1.GA

Mapping documents: None: using AnnotationConfiguration

Code between sessionFactory.openSession() and session.close(): Spring's HibernateTransactionManager

Full stack trace of any exception that occurs: Doesn't: that's the problem...

Name and version of the database you are using: HS@LDB 1.8.0.7 in memory

The generated SQL (show_sql=true): Not relevant

Debug level Hibernate log excerpt: Haven't produced one yet

I understand Hibernate's lazy initialization of Collections. This isn't that.

What I'm seeing is that Hibernate isn't even loading the columns in the top-level entity table until they're accessed. Load an entity that isn't related to any other entities, detach it from its Session, and try to look at its contents, and you get exceptions complaining of no Session.

Not a horribly big deal, I figured: I had an object-graph traverser lying around anyway, so I figured I'd just traverse every object that came in and look at every field I wanted populated while the Session was still open, before I passed it up out of the service layer.

I knew something was wrong, but I figured it'd be easier to track down once I had an end-to-end going.

However, now I've fetched up against something I can't handle: doing a find () on a nonexistent ID. According to the documentation I've read, the Spring Hibernate machinery is supposed to throw an ObjectRetrievalFailureException when I try that. Cool...except that it doesn't. When I find () on a nonexistent ID, I get back a proxy object just like I do when I find () on an existing ID. When I look at its non-Collection fields, they resolve to null. But when I look at its Collection fields, I get an ObjectNotFoundException.

I can't reasonably adapt to this behavior: I need either an immediate exception or a null when an id is not found.

Anybody see anything like this before?

Thanks,
Dan


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 25, 2009 3:17 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
What you are describing seems to an expected behaviour of Hibernate if you are using Session.load() method to load an object by ID. From the javadoc of Session.load():

Quote:
Return the persistent instance of the given entity class with the given identifier, assuming that the instance exists.

You should not use this method to determine if an instance exists (use get() instead). Use this only to retrieve an instance that you assume exists, where non-existence would be an actual error.


Another way to load an object by ID is to use Session.get(), which checks with the database if there is an object or not. Session.get() doesn't throw an exception if the entity doesn't exists. It returns 'null' instead. I don't know if Spring uses the get() or load() method. Maybe it is possible to override/configure which one to use.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 25, 2009 5:02 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
And in case you are using JPA: entityManager.find() is equivalent to session.get(), entityManager.getReference() to session.load();

_________________
-----------------
Need advanced help? http://www.viada.eu


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 25, 2009 10:18 pm 
Newbie

Joined: Wed Feb 25, 2009 12:01 am
Posts: 2
Thanks, guys: I'll bet that does it.


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.