Hibernate version: 3.1.3
I have a class "A" with a simple Integer property as identifier.
It has a joined-subclass "B", which has a many-to-one self-reference, called "previousB".
The application has 3 tiers (in separate VMs), and the middle tier accesses the Hibernate domain model serialized from the backend tier.
Code:
b.getId()
b.getAnySimpleProperty()
goes fine.
Code:
b.getPreviousB().getId()
throws a LazyInitializationException.
I know the "previousB" is not initialized, and hence represented by a generated proxy class. But I also read in "Hibernate in Action" (p.150, Initializing lazy associations) that:
Quote:
A proxy or collection wrapper is automatically initialized when any of its methods are invoked (except the identifier property getter, which may return the identifier value without fetching the underlying persistent object). However, it's only possible to initialize a proxy or collection wrapper if it's currently associated with an open Session. If you close the session and try to access an uninitialized proxy or collection, Hibernate throws a runtime exception.
Here is my stacktrace:
Code:
org.hibernate.LazyInitializationException: could not initialize proxy - no Session
at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:53)
at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:98)
at org.hibernate.proxy.CGLIBLazyInitializer.intercept(CGLIBLazyInitializer.java:158)
at my.package.B$$EnhancerByCGLIB$$d43b234e.getId(<generated>)
... (my code calling b.getPreviousB().getId() )
Given the quote from "Hibernate in Action" I would have thought that "previousB" did
not need to be initialized when the middle tier only needs to access the identifier property!
Can anyone confirm this? And perhaps give hints why I am not succesful...
Or I am totally wrong - nothing can be accessed once the session is gone? (although accessing it within the session would not necessarily perform actual initialization of my class)