Sleep mode: lazy-load does not work on one-to-many mapping on a set

I am using Spring with Hibernate to develop a portlet for the Liferay portal portal. Now I have basically two entities, A and B, where A possibly contains a lot of B. So this happens with the 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 object A in the DAO layer, I inherit the "HibernateDaoSupport" provided by spring, and so a typical data lookup looks like this:

...
public A getA(long id) {
  return (A) getHibernateTemplate().get(A.class, id);
}
...

Everything works fine if I have "lazy = false", but as soon as I switch to "lazy = true", it causes the following error:

org.hibernate.LazyInitializationException: failed to lazily initialize role collection: com.lifepin.entities.PinboardEntry.com, session or session closed

- , , ?

!

+3
4

, , . , .

: Hibernate, , . Hibernate .

Hibernate,

+3
+1

, Hibernate - getHibernateTemplate. lazy = "true", seeions.open() session.close(). lazy = "false" , Hibernate B A.

0

Some people use OpenSessionInViewFilter when using hibernate with the spring framework.

It says that you need to declare a filter in the 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 that does not solve my problem. Maybe I'm doing something wrong.

0
source

Source: https://habr.com/ru/post/1706947/


All Articles