Announcement Announcement Module
No announcement yet.
LazyInitializationException inside @Transactional annotated Service Method Page Title Module
Move Remove Collapse
Conversation Detail Module
  • 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:

    	<bean id="projectEMF"
    		<property name="dataSource" ref="projectDS" />
    		<property name="jpaVendorAdapter" ref="jpaVendorAdapter" />
    	<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    		<property name="entityManagerFactory" ref="projectEMF" />
    		<property name="dataSource" ref="projectDS" />
    	<bean id="jpaVendorAdapter"
    		<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 id="httpSessionEventListener"
    		class="" />
    	<mvc:annotation-driven />
    	<task:annotation-driven />
    	<tx:annotation-driven transaction-manager="transactionManager"/>
    		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.