Hibernate/Spring: failed to lazily initialize - no session or session was closed

China☆狼群 提交于 2019-11-29 19:52:07
Thierry

I think you should not use the hibernate session transactional methods, but let spring do that.

Add this to your spring conf:

<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="mySessionFactory" />
</bean>

<bean id="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate">
    <property name="transactionManager" ref="txManager"/>
</bean>

and then I would modify your test method to use the spring transaction template:

public static void main(String[] args) {
    // init here (getting dao and transaction template)

    transactionTemplate.execute(new TransactionCallback() {
        @Override
        public Object doInTransaction(TransactionStatus status) {
          // do your hibernate stuff in here : call save, list method, etc
        }
    }
}

as a side note, @OneToMany associations are lazy by default, so you don't need to annotate it lazy. (@*ToMany are LAZY by default, @*ToOne are EAGER by default)

EDIT: here is now what is happening from hibernate point of view:

  • open session (with transaction start)
  • save a user and keep it in the session (see the session cache as an entity hashmap where the key is the entity id)
  • save an event and keep it in the session
  • save another event and keep it in the session
  • ... same with all the save operations ...

  • then load all users (the "from Users" query)

  • at that point hibernate see that it has already the object in its session, so discard the one it got from the request and return the one from the session.
  • your user in the session does not have its event collection initialized, so you get null.
  • ...

Here are some points to enhance your code:

  • in your model, when collection ordering is not needed, use Set, not List for your collections (private Set events, not private List events)
  • in your model, type your collections, otherwise hibernate won't which entity to fetch (private Set<Event> events)
  • when you set one side of a bidirectional relation, and you wish to use the mappedBy side of the relation in the same transaction, set both sides. Hibernate will not do it for you before the next tx (when the session is a fresh view from the db state).

So to address the point above, either do the save in one transaction, and the loading in another one :

public static void main(String[] args) {
    // init here (getting dao and transaction template)
    transactionTemplate.execute(new TransactionCallback() {
        @Override
        public Object doInTransaction(TransactionStatus status) {
          // save here
        }
    }

    transactionTemplate.execute(new TransactionCallback() {
        @Override
        public Object doInTransaction(TransactionStatus status) {
          // list here
        }
    }
}

or set both sides:

...
event1.setUser(user);
...
event2.setUser(user);
...
user.setEvents(Arrays.asList(event1,event2));
...

(Also do not forget to address the code enhancement points above, Set not List, collection typing)

In case of Web application, it is also possible to declare a special Filter in web.xml, that will do session-per-request:

<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>

After that you can lazyload your data anytime during the request.

I got here looking for a hint regarding a similar problem. I tried the solution mentioned by Thierry and it didnt work. After that I tried these lines and it worked:

SessionFactory sessionFactory = (SessionFactory) context.getBean("sessionFactory");
Session session = SessionFactoryUtils.getSession(sessionFactory, true);
TransactionSynchronizationManager.bindResource(sessionFactory, new SessionHolder(session));

Indeed what I'm doing is a batch process that must leverage Spring existings managers/services. After loading the context and doing some invocations I founded the famous issue "failed to lazily initialize a collection". Those 3 lines solved it for me.

The issue is that your dao is using one hibernate session but the lazy load of the user.getName (I assume that is where it throws) is happening outside that session -- either not in a session at all or in another session. Typically we open up a hibernate session before we make DAO calls and don't close it until we are done with all lazy loads. Web requests are usually wrapped in a big session so these problems do not happen.

Typically we have wrapped our dao and lazy calls in a SessionWrapper. Something like the following:

public class SessionWrapper {
    private SessionFactory sessionFactory;
    public void setSessionFactory(SessionFactory sessionFactory) {
        this.hibernateTemplate = new HibernateTemplate(sessionFactory);
    }
    public <T> T runLogic(Callable<T> logic) throws Exception {
        Session session = null;
        // if the session factory is already registered, don't do it again
        if (TransactionSynchronizationManager.getResource(sessionFactory) == null) {
            session = SessionFactoryUtils.getSession(sessionFactory, true);
            TransactionSynchronizationManager.bindResource(sessionFactory, new SessionHolder(session));
        }

        try {
            return logic.call();
        } finally {
            // if we didn't create the session don't unregister/release it
            if (session != null) {
                TransactionSynchronizationManager.unbindResource(sessionFactory);
                SessionFactoryUtils.releaseSession(session, sessionFactory);
            }
        }
    }
}

Obviously the SessionFactory the same SessionFactory that was injected into your dao.


In your case, you should wrap the entire listUserWithEvent body in this logic. Something like:

public List listUserWithEvent() {
    return sessionWrapper.runLogic(new Callable<List>() {
        public List call() {
            List users = hibernateTemplate.find("from User");
            for (User user : users) {
                System.out.println("LIST : " + user.getName() + ":");
                user.getEvents().size();
            }
        }
    });
}

You will need to inject the SessionWrapper instance into your daos.

Interesting!

I had the same problem in a @Controller's @RequestMapping handler method. The simple solution was to add a @Transactional annotation to the handler method so that the session is kept open for the whole duration of the method body execution

Easiest solution to implement:

Within the scope of the session[inside the API annotated with @Transactional], do the following:

if A had a List<B> which is lazily loaded, simply call an API which makes sure the List is loaded

What's that API ?

size(); API of the List class.

So all that's needed is:

Logger.log(a.getBList.size());

This simple call of logging the size makes sure it gets the whole list before calculating the size of the list. Now you will not get the exception !

What worked for us in JBoss was the solution #2 taken from this site at Java Code Geeks.

Web.xml:

  <filter>
      <filter-name>ConnectionFilter</filter-name>
      <filter-class>web.ConnectionFilter</filter-class>
  </filter>
  <filter-mapping>
      <filter-name>ConnectionFilter</filter-name>
      <url-pattern>/faces/*</url-pattern>
  </filter-mapping>

ConnectionFilter:

import java.io.IOException;
import javax.annotation.Resource;
import javax.servlet.*;
import javax.transaction.UserTransaction;

public class ConnectionFilter implements Filter {
    @Override
    public void destroy() { }

    @Resource
    private UserTransaction utx;

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        try {
            utx.begin();
            chain.doFilter(request, response);
            utx.commit();
        } catch (Exception e) { }
    }

    @Override
    public void init(FilterConfig arg0) throws ServletException { }
}

Maybe it would work with Spring too.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!