Reputation: 314
I am using hibernate with jsp-servlet. When I am trying to access child element attribute I am getting following exception:
Servlet.service() for servlet jsp threw exception: org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.ecomm.ultimatesms.messaging.persistence.pojos.Mno.startnumbers, no session or session was closed
at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:380) [:3.3.2.GA]
at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:372) [:3.3.2.GA]
at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:365) [:3.3.2.GA]
at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:108) [:3.3.2.GA]
Here is my hibernate mapping files. I am using hibernate-mapping
default-lazy="false"
<hibernate-mapping default-lazy="false">
<class name="com.ecomm.ultimatesms.messaging.persistence.pojos.Mno" table="mno" schema="public">
<id name="pkmnoid" type="long">
<column name="pkmnoid" />
<generator class="sequence">
<param name="sequence">mno_pkmnoid_seq1</param>
</generator>
</id>
<property name="name" type="string">
<column name="name" length="45" />
</property>
<set name="startnumbers" table="startnumber" inverse="false" lazy="true" fetch="select" cascade="delete">
<key>
<column name="fkmnoid" />
</key>
<one-to-many class="com.ecomm.ultimatesms.messaging.persistence.pojos.Startnumber" />
</set>
</class>
</hibernate-mapping>
Upvotes: 1
Views: 3930
Reputation: 16519
One nice way to grant that you still have an open session/entity manager open is to have a filter that injects the EntityManager at the begining of the request than closes its after its execution so youll never care about it anymore. Take a look
package com.renatogama.infra.filters;
import java.io.IOException;
import javax.persistence.EntityManager;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
@WebFilter(urlPatterns = "/*")
public class EntityManagerFilter implements Filter {
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
EntityManager em = new JPAUtil().getEntityManager();
try {
em.getTransaction().begin();
request.setAttribute("em", em);
chain.doFilter(request, response);
em.getTransaction().commit();
} catch (Exception e) {
em.getTransaction().rollback();
throw new ServletException(e);
} finally {
em.close();
}
}
@Override
public void destroy() {}
@Override
public void init(FilterConfig arg0) throws ServletException {}
}
Upvotes: 1
Reputation: 18639
I don't think you need to set all your connection as EAGER.
Alternatively, you need to use Open Session In View Pattern.
You can read more about it here:
http://community.jboss.org/wiki/OpenSessionInView
Upvotes: 2
Reputation: 3082
Though you set lazy='true'
, you described that you set it to false
. So, that causing the issue as session got closed before you accessing the collection(startnumbers
). Either change your mapping by setting lazy='false'
(or) use Hibernate.initialize()
api to load the collection before session close (or) use Open-Session-In-View
pattern.
Upvotes: 0
Reputation: 5602
Since you have set lazy=true
you have to initialize the collection before closing the hibernate session. Here is an article that might help you http://community.jboss.org/wiki/LazyInitializationExceptionovercome
Upvotes: 0