Hi people, I am still struggling with the LazyInitializationException using Spring, Hibernate and JPA.
This thread is in reference to the original thread:
http://forum.springsource.org/showthread.php?t=76488
Basically, I have cracked open the code and have some questions.
In JpaTransactionManager, there is a function called doBegin. In the doBegin method, the workflow is always going into the first condition where this method is always fired: createEntityManagerForTransaction().
Code:if (txObject.getEntityManagerHolder() == null || txObject.getEntityManagerHolder().isSynchronizedWithTransaction()) { EntityManager newEm = createEntityManagerForTransaction(); if (logger.isDebugEnabled()) { logger.debug("Opened new EntityManager [" + newEm + "] for JPA transaction"); } txObject.setEntityManagerHolder(new EntityManagerHolder(newEm), true); }
Inside function createEntityManagerForTransaction(), I noticed that the original EntityManagerFactory (org.springframework.orm.jpa.LocalContainerEntityM anagerFactoryBean@1dc0d) defined in my data-access-context is overwritten for this: org.hibernate.ejb.EntityManagerFactoryImpl@1b3251d !
In other words, the code asks for the original EntityManagerFactyory: getEntityManagerFactory which is cool. However, it then checks if it is an instance of EntityManagerFactoryInfo which is true. And because it is true, it is then overwritten by getNativeEntityManagerFactory!!Code:EntityManagerFactory emf = getEntityManagerFactory(); if (emf instanceof EntityManagerFactoryInfo) { emf = ((EntityManagerFactoryInfo) emf).getNativeEntityManagerFactory(); }
The Session is bound to this new EntityManagerFactory and EntityManager.
Maybe this is why I cannot access this same session again (and get the LazyLoadingException)?
Basically from what I see, there is a conflict of EntityManagerFactories. There is the original one that is created but it is overwritten by getNativeEntityManagerFactory().
The funny thing is this code seems to be always fired, so whats the point of OpenSessionInViewFilter/OpenEntityManagerInViewFilter???
I have no applicationContext.xml in my WAR project (i.e transactionManager is defined once, no duplication). Instead I have my transactionManager hooked up in my data-access-context in web.xml like so:
I know this accesses my transactionManager because if I remove this definition from web.xml, the application complains there is no entityManagerFactory.Code:<context-param> <param-name>contextConfigLocation</param-name> <param-value> classpath:data-access-context.xml </param-value> </context-param>
My data-access-context looks like this:
I shouldn't have to go to such lengths to get OpenEntityManagerInViewFilter working.Code:<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean "> <property name="persistenceUnitName" value="dataAccessPU" /> </bean> <!-- class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean"> --> <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> <property name="entityManagerFactory" ref="entityManagerFactory" /> </bean>
I believe at the root of it, it is a configuration issue and I am missing something...but I do not know what! I have spent hours and hours on this issue taking in all the tips, tricks and advice and have made some adjustments....but am still stuck.
I also see other users on here who are battling the same issue.
Can someone gimme a hand?
Greatly appreciated.
Here is the LazyInitializationException:
Code:2009-09-06 13:39:47,531 DEBUG [http-80-1](org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:730)) - Initiating transaction commit 2009-09-06 13:39:47,531 DEBUG [http-80-1](org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:451)) - Committing JPA transaction on EntityManager [org.hibernate.ejb.EntityManagerImpl@1dd2519] 2009-09-06 13:39:47,546 DEBUG [http-80-1](org.springframework.orm.jpa.JpaTransactionManager.doCleanupAfterCompletion(JpaTransactionManager.java:534)) - Closing JPA EntityManager [org.hibernate.ejb.EntityManagerImpl@1dd2519] after transaction 2009-09-06 13:39:47,546 DEBUG [http-80-1](org.springframework.orm.jpa.EntityManagerFactoryUtils.closeEntityManager(EntityManagerFactoryUtils.java:313)) - Closing JPA EntityManager 2009-09-06 13:39:47,546 DEBUG [http-80-1](com.logixplayer.pf.category.controller.CategoryController.onSubmit(CategoryController.java:42)) - category: Autos 2009-09-06 13:39:47,546 ERROR [http-80-1](org.hibernate.LazyInitializationException.<init>(LazyInitializationException.java:19)) - failed to lazily initialize a collection of role: com.logixplayer.pf.domain.Category.categories, no session or session was closed org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.logixplayer.pf.domain.Category.categories, no session or session was closed at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:358) at 80-1](org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:588)) - Could not complete request org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.logixplayer.pf.domain.Category.categories, no session or session was closed at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:358) at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:350) at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:343) at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:86) at org.hibernate.collection.PersistentSet.iterator(PersistentSet.java:163) at com.logixplayer.pf.category.controller.CategoryController.onSubmit(CategoryController.java:45) at org.springframework.web.servlet.mvc.SimpleFormController.onSubmit(SimpleFormController.java:409) at org.springframework.web.servlet.mvc.SimpleFormController.onSubmit(SimpleFormController.java:381) at org.springframework.web.servlet.mvc.SimpleFormController.processFormSubmission(SimpleFormController.java:267) at org.springframework.web.servlet.mvc.AbstractFormController.handleRequestInternal(AbstractFormController.java:265) at org.springframework.web.servlet.mvc.AbstractController.handleRequest(AbstractController.java:153) at org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:48) at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:875) at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:807) at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:571) at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:511) at javax.servlet.http.HttpServlet.service(HttpServlet.java:637) at javax.servlet.http.HttpServlet.service(HttpServlet.java:717) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter.doFilterInternal(OpenEntityManagerInViewFilter.java:112) at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at 2009-09-06 13:39:47,546 DEBUG [http-80-1](org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:588)) - Could not complete request org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.logixplayer.publicfountain.domain.Category.categories, no session or session was closed at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:358) at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:350) org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583) at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:454) at java.lang.Thread.run(Thread.java:595) 2009-09-06 13:39:47,546 DEBUG [http-80-1](org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter.doFilterInternal(OpenEntityManagerInViewFilter.java:119)) - Closing JPA EntityManager in OpenEntityManagerInViewFilter 2009-09-06 13:39:47,546 DEBUG [http-80-1](org.springframework.orm.jpa.EntityManagerFactoryUtils.closeEntityManager(EntityManagerFactoryUtils.java:313)) - Closing JPA EntityManager


Reply With Quote


