gpt4 book ai didi

hibernate - 带有 session 作用域 bean 的 LazyInitializationException

转载 作者:行者123 更新时间:2023-12-04 02:19:50 25 4
gpt4 key购买 nike

我的服务层中的 session 范围 bean 出现 LazyInitializationException。如果我在我的方法中使用常规 dao 加载相同的 bean,我可以毫无问题地访问它的惰性集合。但是,如果我将它注入(inject)到我的服务 bean 中,然后尝试访问它的惰性集合之一,则会出现 LazyInitializationException。

我正在使用 JPA + Hibernate + Spring + struts。我已经配置了 OpenEntityManagerInViewFilter。此外,我在日志中可以清楚地看到打开了事务和 session 。

在配置具有惰性集合的 session 范围 bean 时,我必须做一些特别的事情吗?

这是日志:

    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)

下面是bean的配置:

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

最佳答案

查看此线程:

Spring Forum Discussion

那么试试这个 - 真的是一样的答案

Better Spring Forum Discussion

似乎最简单的解决方案是在您的 hibernate 映射文件中设置 lazy="false",但那里有一个完整的答案可以让您使用 LazyInitialization

关于hibernate - 带有 session 作用域 bean 的 LazyInitializationException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/918196/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com