4

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!

4 Answers 4

3

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

Sign up to request clarification or add additional context in comments.

4 Comments

I 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.
It 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 ?
I've now switched on Spring's HibernateTransactionManager and added the org.springframework.transaction.interceptor.TransactionProxyFactoryBean before my service class...still the same problem :(
I 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 use Session.lock(myA) + Hibernate#initialize(myA.getComments()).
1

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.

1 Comment

I'm german speaking, so no problem :) I'll take a look at it, thx
0

Some people using OpenSessionInViewFilter when using hibernate with spring framework.

It says you should have to declare filter in your web.xml file:

<filter>
    <filter-name>openSessionInViewFilter</filter-name>
    <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>openSessionInViewFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

But it doesn't solve my problem. Maybe I'm doing something wrong.

Comments

0

This is because your Hibernate session is geting closed somewhere in getHibernateTemplate method, If you have lazy="true" then association can be instantiated between seesion.open() and session.close(). lazy="false" is working because Hibernate is eagerly fetching the associated B objects while loading A.

1 Comment

Your English is really hard to read! I understand that the problem is that my session gets closed somewhere (that's also what the error messages says) and therefore lazy-loading doesn't work obviously. But the point is, how I can avoid to have my session closed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.