Hibernate中的session和load延迟载入矛盾问题,怎样解决?


假设延迟载入出现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来使得延迟载入变得非常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透明地关闭了会话。应用层的代码不用了解不论什么持久层逻辑,还是实现了延迟载入。


原文地址:https://www.cnblogs.com/yfceshi/p/7217287.html