您的位置:首页 > 其它

Hibernate中的session和load延迟加载矛盾问题,如何解决?

2016-03-29 20:17 579 查看
如果延迟加载出现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 Long(1));


if(!Hibernate.isInitialized(customer))


Hibernate.initialize(customer);


tx.commit();


session.close();


customer.getName();

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

即使在视图外面,Spring框架也通过使用AOP 拦截器 HibernateInterceptor来使得延迟加载变得很容易实现。这个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透明地关闭了会话。应用层的代码不用了解任何持久层逻辑,还是实现了延迟加载。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: