-->
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.  [ 1 post ] 
Author Message
 Post subject: lazy init exception after reattachment of detached object
PostPosted: Sat May 15, 2010 3:12 pm 
Newbie

Joined: Sat May 15, 2010 3:04 pm
Posts: 1
Ran into the following puzzling issue, suppose we have a relationship A <-- B <-- C where B is the children of A, C is the children of B. Let's say for now everything is lazily initialized (lazy="true")


Suppose the an object of class C is retrieved in one session, becomes detached, then reattached in another session as such:

Code:

C detachedC = null;

Session session1 = sessionFactory.openSession();

detachedC = session.get(C.class, new Long(123));

session1.close();

Session session2 = sessionFactory.openSession();

session2.update(detachedC); <-- reattach back to session

detachedC.getB().getA().getProperty1();  <-- works sometimes, but not always??
session2.close();


I was expecting walking the object reference such as

Code:
detachedC.getB().getA().getProperty1();


should work, after all I re-attached the object "detachedC" back into the session by calling session.update() on it, then proceeded to walk the object. Hibernate sees that I am attempting to access B through C, then A through B, and proceeds to initialize those objects for me. Everything is fine so far.


However, as soon as I define C --> B as lazy="false" in the mapping, the same exact code gets LazyInitializationException, no session or session was closed Exception.

Why would this be the case? The code is running with the boundary of a new session, so why would it still complain about having no session? This only seems to happen with a Detached Object that has just been re-attached back in the session.


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

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.