views:

3256

answers:

5

Hi,

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!

+1  A: 

When exactly do you get this error ?

How did you configure the connection-releasemode in Hibernate ?

Frederik Gheysels
sorry for the late answer. Actually I didn't configure it at all. Should I?
Juri
+2  A: 

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

Tom Carter
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.
Juri
Tom Carter
I've now switched on Spring's HibernateTransactionManager and added the org.springframework.transaction.interceptor.TransactionProxyFactoryBean before my service class...still the same problem :(
Juri
@Tom: 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())`.
dma_k
A: 

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.

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.
Juri
A: 

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.

A: 

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.

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