Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>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 <em>before</em> 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.</p> <p>Typically we have wrapped our dao and lazy calls in a SessionWrapper. Something like the following:</p> <pre><code>public class SessionWrapper { private SessionFactory sessionFactory; public void setSessionFactory(SessionFactory sessionFactory) { this.hibernateTemplate = new HibernateTemplate(sessionFactory); } public &lt;T&gt; T runLogic(Callable&lt;T&gt; 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); } } } } </code></pre> <p>Obviously the SessionFactory the same SessionFactory that was injected into your dao.</p> <hr> <p>In your case, you should wrap the entire listUserWithEvent body in this logic. Something like:</p> <pre><code>public List listUserWithEvent() { return sessionWrapper.runLogic(new Callable&lt;List&gt;() { public List call() { List users = hibernateTemplate.find("from User"); for (User user : users) { System.out.println("LIST : " + user.getName() + ":"); user.getEvents().size(); } } }); } </code></pre> <p>You will need to inject the SessionWrapper instance into your daos.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload