Hibernate: lazy-loading doesn't work on one-to-many mapping on set
Solution 1
You usually get this error if you have loaded the object in one request, then without accessing the lazy loaded property, saved the object in session. If you then in a different request try to access the lazy loaded property you will get this exception.
Simply put : the Hibernate session in which the object was initially loaded has closed. On accessing the lazily loaded property of this object in a different Hibernate session causes this exception.
You will have to reload your object into the current Hibernate session to be able lazily load a property
Solution 2
A good discussion on hibernate lazy loading and a very helpful solution (called Preload pattern) can be found here: http://entwickler-forum.de/showthread.php?t=47067
Unfortunately this is a German website. But at least the source code and its doc is in English.
The core idea of the website above is to give an opportunity to avoid loading the whole object graph (via lazy loading) and to explicitly specify which parts of the object graph should be loaded in a given situation.
Related videos on Youtube
abhijeet nigoskar
Architect 👨💻 @nrwl_io 🐳🦄 • Google Dev Expert #GDE •🎓 @eggheadio • ❤ JS, Angular • 📝 blogger • 🗣️ speaker • @cypress_io Ambassador • nx.app • nx.dev You can find me on juri.dev or @juristr
Updated on April 17, 2022Comments
-
abhijeet nigoskar about 2 years
I'm using Spring together with Hibernate for developing a Portlet for the Liferay portal server. I now have basically two entities, A and B, where A possibly contains many B's. So this goes to a one-to-many mapping between the two.
<set cascade="all" lazy="true" name="comments" order-by="creationDate desc"> <key column="lfpn_pinboardentries_idPinboardEntry" not-null="true"/> <one-to-many class="Comment"/> </set>
In the corresponding DAO of entity A in the DAO layer, I'm inheriting from "HibernateDaoSupport" provided by spring, and so a typical retrieval of data looks like the following:
... public A getA(long id) { return (A) getHibernateTemplate().get(A.class, id); } ...
Everything works fine if I'm having "lazy=false", but as soon as I'm switching to "lazy=true" it gives me the following error:
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.lifepin.entities.PinboardEntry.comments, no session or session was closed
Does anyone have a suggestion what could be the problem or hints how to solve it?
Thanks!
-
abhijeet nigoskar about 15 yearsI don't think the problem is the request since I get the same problem on my unit tests where I'm just loading the parent object and then when trying to access the list of inteded lazy-loaded objects, it breaks down, giving me the error message of above.
-
Tom Carter about 15 yearsIt still seems to me that your initial hibernate session is being closed before you access the lazily loaded collection. Turn on logging to verify. Another thing to look at though is the transaction config of Spring & do you use the [Transaction()] attribute for your method ?
-
abhijeet nigoskar almost 15 yearsI've now switched on Spring's HibernateTransactionManager and added the org.springframework.transaction.interceptor.TransactionProxyFactoryBean before my service class...still the same problem :(
-
abhijeet nigoskar almost 15 yearsI'm german speaking, so no problem :) I'll take a look at it, thx
-
dma_k over 13 yearsI would rather say: You need to open the session and re-associate the object with a session via
Session.merge()
before accessing the lazy collection. Alternatively you can useSession.lock(myA)
+Hibernate#initialize(myA.getComments())
.