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.