Announcement Announcement Module
Collapse
No announcement yet.
LazyInitializationException inside @Transactional annotated Service Method Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • LazyInitializationException inside @Transactional annotated Service Method

    Using Spring 3.0.5, Hibernate 3.6 and JPA 2.0.

    I have a Service (annotated with @Service) and an injected DAO (annotated with @Repository). The service has a method with @Transactional (propagation = REQUIRED).

    Inside the method an Entity is loaded (using the DAO), the entity has a lazy collection, still inside the method when an item in the collection is accessed LazyInitializationException is thrown.

    How come?

    The method has @Transactional, so, in theory the transaction should still be alive, why can't hibernate attach to the current TX?

    I'm pretty sure I'm missing something in the configuration:

    Code:
    	<bean id="projectEMF"
    		class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    		<property name="dataSource" ref="projectDS" />
    		<property name="jpaVendorAdapter" ref="jpaVendorAdapter" />
    	</bean>
    
    	<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    		<property name="entityManagerFactory" ref="projectEMF" />
    		<property name="dataSource" ref="projectDS" />
    	</bean>
    
    	<bean id="jpaVendorAdapter"
    		class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
    
    		<property name="database" value="SQL_SERVER" />
    		<property name="showSql" value="false" />
    		<property name="generateDdl" value="false" />
    		<property name="databasePlatform" value="org.hibernate.dialect.SQLServerDialect" />
    	</bean>
    
    	<bean id="httpSessionEventListener"
    		class="ca.utoronto.med.dc.focus.security.SimpleHttpSessionEventListenerImpl" />
    
    	<mvc:annotation-driven />
    	<task:annotation-driven />
    	<tx:annotation-driven transaction-manager="transactionManager"/>
    
    	<context:annotation-config
    		transaction-manager="transactionManager" proxy-target-class="false" />
    Any pointers will be greatly appreciated.

  • #2
    Judging from your configuration you heavily use annotations, so I suspect you also use component-scan. make sure you don't have multiple component-scan elements scanning the same package (once for your contextloaderlistener and once for your dispatcherservlet) as that will lead to duplicate beans with all sorts of issues...

    Another problem could be your DAO and how you implemented it, post some code.

    Comment

    Working...
    X