LazyInitializationException with bean session scope

I get a LazyInitializationException with a bean session scope in my service. If I load the same bean with the usual dao in my method, I can easily get lazy collections for it. But if I inject it into my bean service and then try to access one of my lazy collections, I have a LazyInitializationException.

I am using JPA + Hibernate + Spring + struts. I have configured OpenEntityManagerInViewFilter. Moreover, I can clearly see in the logs that the transaction and session are open.

Is there anything special I need to do in the bean configuration for sessions 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 is the bean configuration:

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

:

Spring

, -

Spring

, lazy = "false" , , LazyInitialization

+1

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

+1

, . , . .

, : , , UserBean - , - , . .

0
source

Source: https://habr.com/ru/post/1709241/


All Articles