作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有 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/
Haskell 有一个非常膨胀的 undefined 值,它会惰性地引发异常(在求值时)。 Ocaml当然是严格的,所以as far as I can tell there is no equival
COW 不是奶牛,是 Copy-On-Write 的缩写,这是一种是复制但也不完全是复制的技术。 一般来说复制就是创建出完全相同的两份,两份是独立的: 但是,有的时候复制这件事没多大必要
我是一名优秀的程序员,十分优秀!