views:

1609

answers:

2

I'm getting LazyInitializationException with session scoped bean in my service layer. If I load the same bean using a regular dao in my method, I can access it's lazy collections without problems. But if I inject it in my service bean, and then try to access one of its lazy collection, I have a LazyInitializationException.

I'm using JPA + Hibernate + Spring + struts. I have configured OpenEntityManagerInViewFilter. Futhermore, I can clearly see in the logs that the transaction and the session are opened.

Is there something special that I have to do in the configuration for session-scoped bean with lazy collections?

Here are the logs:

    org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter:lookupEntityManagerFactory:146 - Using EntityManagerFactory 'entityManagerFactory' for OpenEntityManagerInViewFilter
    org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter:doFilterInternal:101 - Opening JPA EntityManager in OpenEntityManagerInViewFilter
    org.springframework.orm.jpa.JpaTransactionManager:doGetTransaction:285 - Found thread-bound EntityManager [org.hibernate.ejb.EntityManagerImpl@17ab5c0] for JPA transaction
    org.springframework.transaction.support.AbstractPlatformTransactionManager:getTransaction:371 - Creating new transaction with name [com.xx.action.spring.service.SearchService.loadCurrencyCode]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT,readOnly
    org.hibernate.impl.SessionImpl:<init>:247 - opened session at timestamp: 5093202578464768
    org.hibernate.transaction.JDBCTransaction:begin:82 - begin
    org.hibernate.jdbc.ConnectionManager:openConnection:444 - opening JDBC connection
    org.hibernate.transaction.JDBCTransaction:begin:87 - current autocommit status: true
    org.hibernate.transaction.JDBCTransaction:begin:90 - disabling autocommit
    org.springframework.orm.jpa.JpaTransactionManager:doBegin:348 - Exposing JPA transaction as JDBC transaction [SimpleConnectionHandle: com.mchange.v2.c3p0.impl.NewProxyConnection@9b537f]
    org.hibernate.LazyInitializationException:<init>:42 - could not initialize proxy - no Session
    org.hibernate.LazyInitializationException: could not initialize proxy - no Session
        at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:86)
        at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:140)
        at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:190)
        at com.xxx.api.jpa.bean.CurrencyBean_$$_javassist_29.getHtmlSymbol(CurrencyBean_$$_javassist_29.java)

Here are the configuration of the bean:

<bean id="currentUserBean" class="com.xxx.action.spring.CurrentUserBean" scope="session">
     <aop:scoped-proxy /> 
</bean>
+1  A: 

Check out this thread:

Spring Forum Discussion

Well then try this one - really the same answer

Better Spring Forum Discussion

Seems the simplest solution is to set lazy="false" in your hibernate mapping file, but there is a full answer there that will allow you to use LazyInitialization

Gandalf
Thanks but I'm not using JSF. I'm using Struts.
Thierry-Dimitri Roy
A: 

I think you should have scope as request. Reason is your Transaction will be active only for a Request not for a Session. Thats why you are seeing lazy Initialization Exception.

Also your doubt of Transaction being active: I think explanation for this will be that UserBean was created in some other Transaction and when you are accessing in some different Transaction its saying session closed. As session would have got closed with First Transaction Completion.

Pavitar Singh