假设延迟载入出现session close的情况下

方法1.在web.xml中配置spring的openSessionInViewFilter

<filter>

 <filter-name>hibernateFilter</filter-name>

 <filter-class>

 org.springframework.orm.hibernate3.support.OpenSessionInViewFilter

 </filter-class>

</filter

<filter-mapping>

 <filter-name>hibernateFilter</filter-name>

 <url-pattern>*.do</url-pattern>

</filter-mapping>

方法2.在详细代码中

tx = session.beginTransaction();

Customer customer=(Customer)session.load(Customer.class,new));

if(!Hibernate.isInitialized(customer))

Hibernate.initialize(customer);

tx.commit();

session.close();

customer.getName();

在业务逻辑层中使用延迟载入

即使在视图外面,Spring框架也通过使用AOP 拦截器 HibernateInterceptor来使得延迟载入变得非常easy实现。这个Hibernate 拦截器透明地将调用配置在Spring应用程序上下文中的业务对象中方法的请求拦截下来。在调用方法之前打开一个Hibernate会话。然后在方法运行完之后将会话关闭。

让我们来看一个简单的样例。如果我们有一个接口

BussinessObject:

public interface BusinessObject {

public void doSomethingThatInvolvesDaos();

 }

类BusinessObjectImpl实现了BusinessObject接口:



public class BusinessObjectImpl implements BusinessObject {

public void doSomethingThatInvolvesDaos() {

// lots
of logic that calls

// DAO
classes Which access

// data
objects lazily 

 } 

 }

通过在Spring应用程序上下文中的一些配置。我们能够让将调用BusinessObject的方法拦截下来,再令它的方法支持延迟载入。看看以下的一个程序片段:

<beans>

<bean id="hibernateInterceptor" class="org.springframework.orm.hibernate.HibernateInterceptor">

<property name="sessionFactory">

<ref bean="sessionFactory"/>

</property>

</bean>

<bean id="businessObjectTarget" class="com.acompany.BusinessObjectImpl">

<property name="someDAO"><ref bean="someDAO"/></property>

</bean>

<bean id="businessObject" class="org.springframework.aop.framework.ProxyFactoryBean">

<property name="target"><ref bean="businessObjectTarget"/></property>

<property name="proxyInterfaces">

<value>com.acompany.BusinessObject</value>

</property>

<property name="interceptorNames">

<list>

<value>hibernateInterceptor</value>

</list>

</property>

</bean>

</beans>

当businessObject被调用的时候,HibernateInterceptor打开一个Hibernate会话,并将调用请求传递给 BusinessObjectImpl对象。当BusinessObjectImpl运行完毕后,HibernateInterceptor透明地关闭了会话。应用层的代码不用了解不论什么持久层逻辑,还是实现了延迟载入。

Hibernate中的session和load延迟载入矛盾问题,怎样解决?的更多相关文章

  1. 第四讲 :hibernate中的session

    hibernate中的session中可以进行增删改差,通过工具类可以得到相关的工具类. 方法概要:  Transaction beginTransaction()开始一个工作单元,得到关联的事务对象 ...

  2. Hibernate中的Session

    我们之前也经常使用Session,通过连接服务器将需要保存的值存到服务器的session中,这是之前关于session的简单应用.现在看到Hibernate框架中也有关于Session的定义,该定义是 ...

  3. Hibernate中的Session对象 标签: hibernatesession 2017-01-22 22:10 238人阅读 评论(

    Hibernate中的Session 大家在看hibernate视频的时候一定都发现了,每次要操作数据库,总是要新建一个session对象,Hibernate在对资料库进行操作之前,必须先取得Sess ...

  4. 关于hibernate中的session与数据库连接关系以及getCurrentSession 与 openSession() 的区别

    1.session与connection,是多对一关系,每个session都有一个与之对应的connection,一个connection不同时刻可以供多个session使用.   2.多个sessi ...

  5. (八) Hibernate中的Session以及事务

    HibernateUtil.getSessionFactory().getCurrentSession() 和HibernateUtil.getSession() 的区别: 1.异:getCurren ...

  6. Hibernate中的Session缓存问题

    1. Session 缓存: 1) . 在 Session 接口的实现中包括一系列的 Java 集合 , 这些 Java 集合构成了 Session 缓存 .          它用于存放 Sessi ...

  7. Hibernate-ORM:04.Hibernate中的get()和load()

    ------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- 本篇博客会讲如何用get()或load()查询单个对象和对缓存的简单操作,以及他俩的区别和相同(前面有的那些配 ...

  8. Hibernate中的session对象update方法的使用

    使一个游离对象转变为持久化对象.例如以下代码在session1中保存了一个Customer对象,然后在session2中更新这个Customer对象: Customer customer = new ...

  9. hibernate 中的session和事务(Transaction)

    在使用hibernate开发时,遇到最多的就是session与事务,那么他们两个有什么关系呢?下面我来抛砖引玉: 1.session是hibernate中的以及缓存机制,是用来对数据进行增删改查的一个 ...

随机推荐

  1. 聊聊、Spring 第一篇

    Spring 大家都不陌生,企业应用中很流行的一个平台.从最开始的 Servlet 控制所有,到 MVC 模式的出现.从 SSH (Struts.Spring.Hibernate) 所谓的三剑客 到 ...

  2. Octave 里的 fminunc

    ptions = optimset('GradObj', 'on', 'MaxIter', '100'); initialTheta = zeros(2,1); [optTheta, function ...

  3. 九度oj 题目1184:二叉树遍历

    题目描述: 编一个程序,读入用户输入的一串先序遍历字符串,根据此字符串建立一个二叉树(以指针方式存储). 例如如下的先序遍历字符串:ABC##DE#G##F###其中“#”表示的是空格,空格字符代表空 ...

  4. iOS------手势操作(nib文件、纯代码)

    总共有六种手势识别:轻击手势(TapGestureRecognizer),轻扫手势 (SwipeGestureRecognizer), 长按手势(LongPressGestureRecognizer) ...

  5. 【bzoj4407】于神之怒加强版 莫比乌斯反演+线性筛

    题目描述 给下N,M,K.求 输入 输入有多组数据,输入数据的第一行两个正整数T,K,代表有T组数据,K的意义如上所示,下面第二行到第T+1行,每行为两个正整数N,M,其意义如上式所示. 输出 如题 ...

  6. 刷题总结——String painter(hdu2476)

    题目: Problem Description There are two strings A and B with equal length. Both strings are made up of ...

  7. Cannot open include file: 'initializer_list': No such file or directory

    Cannot open include file: 'initializer_list': No such file or directory今天使用VS2012编译一个项目的时候,遇到了这个问题,上 ...

  8. [暑假集训--数位dp]LightOj1205 Palindromic Numbers

    A palindromic number or numeral palindrome is a 'symmetrical' number like 16461 that remains the sam ...

  9. uva 103 Stacking Boxes(最长上升子序列)

    Description    Stacking Boxes  Background Some concepts in Mathematics and Computer Science are simp ...

  10. 【NOIP2016练习】T2 花花的聚会 (树形DP,倍增)

    题意: 花花住在 H 国.H 国有 n 个城市,其中 1 号城市为其首都.城市间有 n 1 条单向道路.从任意一个城市出发,都可以沿着这些单向道路一路走到首都.事实上,从任何一个城市走到首都的路径是唯 ...