LazyInitializationException - Spring/Hibernate4/JPA (Resolvido)

5 respostas
R

Olá,

Estou com problema LazyInitializationException na camada do JSP, mas nao estou conseguindo configurar o filtro do spring, gostaria de um auxilio.

Erro:
org.apache.jasper.JasperException: org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: br.com.parede.domain.model.Produto.imagens, no session or session was closed

persistence.xml
<persistence-unit name="secondary">
		<provider>org.hibernate.ejb.HibernatePersistence</provider>
		<jta-data-source>java:jboss/datasources/MysqlDS</jta-data-source>
		<properties>
			<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect"/>
			<property name="jboss.entity.manager.factory.jndi.name" value="java:jboss/spring/persistence" />
			<!-- Properties for Hibernate -->
			<!-- <property name="hibernate.hbm2ddl.auto" value="update" /> -->
			<property name="hibernate.hbm2ddl.auto" value="create-drop" />
			<property name="hibernate.show_sql" value="true" />
			<property name="hibernate.format_sql" value="true" />
			<property name="hibernate.use_sql_comments" value="false" />
		</properties>
   </persistence-unit>
infrastructure.xml
<beans xmlns="http://www.springframework.org/schema/beans"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       	 xmlns:jee="http://www.springframework.org/schema/jee"
       	 xmlns:tx="http://www.springframework.org/schema/tx"
       	 xmlns:mvc="http://www.springframework.org/schema/mvc"
         xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
		   http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
		   http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

    <jee:jndi-lookup jndi-name="java:jboss/spring/persistence" id="entityManagerFactory" expected-type="javax.persistence.EntityManagerFactory" />

	<bean id="entityManager" class="org.springframework.orm.jpa.support.SharedEntityManagerBean">
   		<property name="entityManagerFactory" ref="entityManagerFactory"/>
	</bean>
	
	<!-- <mvc:interceptors>
	    <bean class="org.springframework.orm.hibernate4.support.OpenSessionInViewInterceptor">
	        <property name="sessionFactory">
	            <ref local="entityManager" />
	        </property>
	    </bean>
	</mvc:interceptors> -->

    <tx:jta-transaction-manager />

	
</beans>

Tentei usar o mvc:interceptors, mas quando envio o entityManager ocorre um erro:

14:21:28,566 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/webportal]] (MSC service thread 1-5) Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.web.servlet.handler.MappedInterceptor#0': Cannot create inner bean 'org.springframework.orm.hibernate4.support.OpenSessionInViewInterceptor#0' of type [org.springframework.orm.hibernate4.support.OpenSessionInViewInterceptor] while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.orm.hibernate4.support.OpenSessionInViewInterceptor#0' defined in class path resource [META-INF/spring/infrastructure.xml]: Initialization of bean failed; nested exception is org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type '$Proxy83 implementing javax.persistence.EntityManager,org.springframework.orm.jpa.EntityManagerProxy' to required type 'org.hibernate.SessionFactory' for property 'sessionFactory'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [$Proxy83 implementing javax.persistence.EntityManager,org.springframework.orm.jpa.EntityManagerProxy] to required type [org.hibernate.SessionFactory] for property 'sessionFactory': no matching editors or conversion strategy found
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveInnerBean(BeanDefinitionValueResolver.java:281) [spring-beans-3.1.1.RELEASE.jar:3.1.1.RELEASE]
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:120) [spring-beans-3.1.1.RELEASE.jar:3.1.1.RELEASE]
at org.springframework.beans.factory.support.ConstructorResolver.resolveConstructorArguments(ConstructorResolver.java:616) [spring-beans-3.1.1.RELEASE.jar:3.1.1.RELEASE]
at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:148) [spring-beans-3.1.1.RELEASE.jar:3.1.1.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1035) [spring-beans-3.1.1.RELEASE.jar:3.1.1.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:939) [spring-beans-3.1.1.RELEASE.jar:3.1.1.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:485) [spring-beans-3.1.1.RELEASE.jar:3.1.1.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456) [spring-beans-3.1.1.RELEASE.jar:3.1.1.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294) [spring-beans-3.1.1.RELEASE.jar:3.1.1.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225) [spring-beans-3.1.1.RELEASE.jar:3.1.1.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291) [spring-beans-3.1.1.RELEASE.jar:3.1.1.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193) [spring-beans-3.1.1.RELEASE.jar:3.1.1.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:585) [spring-beans-3.1.1.RELEASE.jar:3.1.1.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:913) [spring-context-3.1.1.RELEASE.jar:3.1.1.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464) [spring-context-3.1.1.RELEASE.jar:3.1.1.RELEASE]
at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:385) [spring-web-3.1.1.RELEASE.jar:3.1.1.RELEASE]
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:284) [spring-web-3.1.1.RELEASE.jar:3.1.1.RELEASE]
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:111) [spring-web-3.1.1.RELEASE.jar:3.1.1.RELEASE]
at org.apache.catalina.core.StandardContext.contextListenerStart(StandardContext.java:3392) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.core.StandardContext.start(StandardContext.java:3850) [jbossweb-7.0.13.Final.jar:]
at org.jboss.as.web.deployment.WebDeploymentService.start(WebDeploymentService.java:90) [jboss-as-web-7.1.1.Final.jar:7.1.1.Final]
at org.jboss.msc.service.ServiceControllerImpl$StartTask.startService(ServiceControllerImpl.java:1811)
at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1746)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) [rt.jar:1.7.0_01]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) [rt.jar:1.7.0_01]
at java.lang.Thread.run(Unknown Source) [rt.jar:1.7.0_01]
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.orm.hibernate4.support.OpenSessionInViewInterceptor#0' defined in class path resource [META-INF/spring/infrastructure.xml]: Initialization of bean failed; nested exception is org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type '$Proxy83 implementing javax.persistence.EntityManager,org.springframework.orm.jpa.EntityManagerProxy' to required type 'org.hibernate.SessionFactory' for property 'sessionFactory'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [$Proxy83 implementing javax.persistence.EntityManager,org.springframework.orm.jpa.EntityManagerProxy] to required type [org.hibernate.SessionFactory] for property 'sessionFactory': no matching editors or conversion strategy found
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:527) [spring-beans-3.1.1.RELEASE.jar:3.1.1.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456) [spring-beans-3.1.1.RELEASE.jar:3.1.1.RELEASE]
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveInnerBean(BeanDefinitionValueResolver.java:270) [spring-beans-3.1.1.RELEASE.jar:3.1.1.RELEASE]
... 25 more
Caused by: org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type '$Proxy83 implementing javax.persistence.EntityManager,org.springframework.orm.jpa.EntityManagerProxy' to required type 'org.hibernate.SessionFactory' for property 'sessionFactory'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [$Proxy83 implementing javax.persistence.EntityManager,org.springframework.orm.jpa.EntityManagerProxy] to required type [org.hibernate.SessionFactory] for property 'sessionFactory': no matching editors or conversion strategy found
at org.springframework.beans.BeanWrapperImpl.convertIfNecessary(BeanWrapperImpl.java:485) [spring-beans-3.1.1.RELEASE.jar:3.1.1.RELEASE]
at org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:516) [spring-beans-3.1.1.RELEASE.jar:3.1.1.RELEASE]
at org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:510) [spring-beans-3.1.1.RELEASE.jar:3.1.1.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.convertForProperty(AbstractAutowireCapableBeanFactory.java:1406) [spring-beans-3.1.1.RELEASE.jar:3.1.1.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1365) [spring-beans-3.1.1.RELEASE.jar:3.1.1.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1118) [spring-beans-3.1.1.RELEASE.jar:3.1.1.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517) [spring-beans-3.1.1.RELEASE.jar:3.1.1.RELEASE]
... 27 more
Caused by: java.lang.IllegalStateException: Cannot convert value of type [$Proxy83 implementing javax.persistence.EntityManager,org.springframework.orm.jpa.EntityManagerProxy] to required type [org.hibernate.SessionFactory] for property 'sessionFactory': no matching editors or conversion strategy found
at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:241) [spring-beans-3.1.1.RELEASE.jar:3.1.1.RELEASE]
at org.springframework.beans.BeanWrapperImpl.convertIfNecessary(BeanWrapperImpl.java:470) [spring-beans-3.1.1.RELEASE.jar:3.1.1.RELEASE]
... 33 more

5 Respostas

D

Como estão os relacionamentos?

H

Leia isso: Quatro soluções para LazyInitializationException

R

é, pelo que parece você tentou implementar algum tipo de Open(Session|EntityManager)InView, mas se bagunçou em algum casting, talvez algum pacote incorreto.

R

achei um outro post que acabou resolvendo meu problema:
http://www.guj.com.br/java/98508-lazyinitializationexception-2-dias-tentando-resolver--resolvido

Eu tinha aplicado o filtro no web.xml e tbm nao tinha resolvido, mas o filtro tem que ficar em primeiro eu tinha outro filtro acima do spring security, invertendo resolveu.

Agora qual a diferença entre o OpenEntityManagerInViewFilter e OpenSessionInViewInterceptor, performace?

R

Dessa forma acho que aconteceu isso mesmo, será precisa implementar outra classe do spring no entityManager?

Criado 30 de agosto de 2012
Ultima resposta 30 de ago. de 2012
Respostas 5
Participantes 4