Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LazyInitializationException with session scoped bean

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>    
like image 975
Thierry-Dimitri Roy Avatar asked Mar 14 '26 21:03

Thierry-Dimitri Roy


2 Answers

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

like image 111
Gandalf Avatar answered Mar 16 '26 15:03

Gandalf


@PersistenceContext(type=PersistenceContextType.EXTENDED) works :)

like image 39
shalunv Avatar answered Mar 16 '26 14:03

shalunv