gpt4 book ai didi

hibernate - Spring + Hibernate 懒加载

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

我有 org.hibernate.LazyInitializationException 问题:无法延迟初始化角色集合。

如何使用 gwt + spring + hibernate 实现延迟获取?

这是我的 appContext:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory">
<ref local="sessionFactory"/>
</property>
</bean>
<context:annotation-config/>
<context:component-scan base-package="com.yeah.server.*"/>
<aop:aspectj-autoproxy/>
<!--Mysql database connection info-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://192.168.1.4:3306/YeaH"/>
<property name="username" value="root"/>
<property name="password" value=""/>
</bean>
<!-- Hibernate -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mappingResources">
<list>
<value>com/yeah/shared/model/User.hbm.xml</value>
<value>com/yeah/shared/model/Comment.hbm.xml</value>
<value>com/yeah/shared/model/Album.hbm.xml</value>
<value>com/yeah/shared/model/Picture.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
</bean>
<!--HIbernate session management
<bean name="openSessionInViewInterceptor"
class="org.springframework.orm.hibernate3.support.OpenSessionInViewInterceptor">
<property name="sessionFactory" ref="sessionFactory"/>
<property name="singleSession" value="false"/>
</bean>
-->
<!-- a PlatformTransactionManager is still required -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
</beans>

最佳答案

发生异常是因为您在加载域对象的 hibernate session 关闭后尝试访问域对象上的延迟加载属性。

解决此问题的常用方法是使用 Spring OpenSessonInViewFilter。这实质上将您的 hibernate session 范围限定为您的 HTTP 请求。然后,在该 HTTP 请求/响应周期内发生的任何属性访问都将在该 session 的范围内。

您在 web.xml 中对其进行如下配置:

<filter>
<filter-name>Spring OpenSessionInViewFilter</filter-name>
<filter-class>
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
</filter-class>
</filter>

希望这可以帮助。

关于hibernate - Spring + Hibernate 懒加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6062413/

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