spring 3.x + hibernate4.x 实现数据延迟加载
来源:互联网 发布:c3p0连接池配置 mysql 编辑:程序博客网 时间:2024/06/05 08:36
Spring为我们解决Hibernate的Session的关闭与开启问题。
Hibernate 允许对关联对象、属性进行延迟加载,但是必须保证延迟加载的操作限于同一个 Hibernate Session 范围之内进行。如果 Service 层返回一个启用了延迟加载功能的领域对象给 Web 层,当 Web 层访问到那些需要延迟加载的数据时,由于加载领域对象的 Hibernate Session 已经关闭,这些导致延迟加载数据的访问异常
(eg: org.hibernate.LazyInitializationExcept
- failed to lazily initialize a collection of role: cn.easyjava.bean.product.ProductType.childtypes, no session or session was closed
org.hibernate.LazyInitializationExcept
<filter-name>openSessionInViewFilter</filter-name>
<filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter </filter-class>
<init-param>
<param-name>sessionFactoryBeanName</param-name>
<param-value>sessionFactory</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>openSessionInViewFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
注意:sessionFactiory 是自己在springContext中定义的 org.springframework.orm.hibernate4.LocalSessionFactoryBean的实例(一般在appricationContext.xml中定义)
方法二:利用org.springframework.orm.hibernate4.support.OpenSessionInViewInterceptor,在appricationContext.xml 中设置
<bean name="openSessionInViewInterceptor"
class="org.springframework.orm.hibernate4.support.OpenSessionInViewInterceptor">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<bean id="urlMapping"
class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="interceptors">
<list>
<ref bean="openSessionInViewInterceptor" />
</list>
</property>
<property name="mappings">
<value>/*</value> <!-- 需要拦截的url -->
</property>
</bean>
0 0
- spring 3.x + hibernate4.x 实现数据延迟加载
- spring 3.x + hibernate4.x 实现数据延迟加载
- Spring4.x+Hibernate4.x整合
- Jquery实现延迟加载数据
- Struts2.x+Spring3.x+Hibernate4.x 整合(二)Struts+Spring 整合
- hibernate4.x编译
- spring 集成 Hibernate4.3.X org.hibernate.service.jta.platform.spi.JtaPlatform异常
- extjs 3.x简单的异步树 并实现展开时重新加载数据的实现
- Hibernate+spring数据延迟加载问题解决方案。
- EF5.X Code First表关联与延迟加载
- Win10开发:使用 x:DeferLoadStrategy 延迟加载元素
- Spring 3.x企业应用开发实战(4)----资源加载
- spring4.x + hibernate4.x 配置详解
- spring4.x + hibernate4.x 配置详解
- Spring3.x + Hibernate4.x 升级注意事项
- spring4.x + hibernate4.x 配置详解
- spring4.x + hibernate4.x 配置详解
- JPA(Hibernate4)+Spring4 延迟加载问题
- 访问高于minSdk版本的API
- 物料主数据的关键表
- jquery弹出层 +CSS箭头制作
- 怎样安装express
- 编译Boost 详细步骤
- spring 3.x + hibernate4.x 实现数据延迟加载
- 关于工资的三个秘密
- 判断给定二叉树是否是二叉搜索树(LeetCode: Validate Binary Search Tree)
- Hive Map Join
- c++ vector各种函数介绍及其用法
- HDU-2568-前进
- Linux系统添加crontab任务无效的问题解决方案
- OS Watcher
- PHPCMS建站新手入门基础视频教程简介