转载地址:http://hi.baidu.com/accpandsvse/item/2f23543e4c027e302e20c4a5
1、说说为什么使用lazy
当使用Hibernate中的one-to-many、many-to one、many-to-many关系映射的时候,一个对象中会包含一个或多个Set来关联其他的对象。例如:user-groups,当程序取user 对象时,如果一个用户有多个自定义组,那么程序将把组的信息也读取出来,在log中可以看到两个sql的输出。但是在页面的显示上,也许并不需要显示这个用户相关组的信息,这样系统的消耗就白白浪费了,于是hibernate提供了lazy(延迟加载)的方法来避免这一情况的发生,我们只需要在 user.hbm.xml中设置lazy=true,就能实现延迟加载。
<set name="groupses" table="usergroups" catalog="many" cascade="save-update" lazy="true">
<key>
<column name="userid" length="32" not-null="true" />
</key>
<many-to-many entity-name="com.example.model.Groups">
<column name="groupid" length="32" not-null="true" />
</many-to-many>
</set>
2、说说为什么使用OpenSessionInView
当hibernate+spring配合使用的时候,如果设置了lazy=true,那么在读取数据的时候,当读取了父数据后,hibernate会自动关闭session,这样,当要使用子数据的时候,系统会抛出lazyinit的错误,这时就需要使用spring提供的 OpenSessionInViewFilter,
OpenSessionInViewFilter主要是保持Session状态知道request将全部页面发送到客户端,这样就可以解决延迟加载带来的问题
3、说说struts2中使用OpenSessionInView的注意事项
1、OpenSessionInViewFilter要在Webwork的filter前面,否则系统会报错。
2、对于OpenSessionInView的配置中,singleSession应该设置为true,表示一个request只能打开一个 session,如果设置为false的话,session可以被打开多个,这时在update、delete的时候会出现打开多个session的异常。但是当设置为true的时候,系统的性能会因为用户的网络状况受到影响,当request在生成页面完成后,session才会被释放,所以如果用户的网络状况比较差,那么连接池中的链接会迟迟不被回收,造成内存增加,系统性能受损。但是如果不用这种方法的话,lazy模式有体现不出它的优点。singleSession默认为true,若设为false则等于没用OpenSessionInView 。所以默认可以不写 。
3、只有在spring中配置了事物才能在web.xml配置openSessionInViewFilter否则会出错, dao层的类都要继承于 HibernateDaoSupport
web.xml配置:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>s2sh</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
</web-app>
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<filter>
<filter-name>openSessionInViewFilter</filter-name>
<filter-class>
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
</filter-class>
<init-param>
<param-name>singleSession</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>openSessionInViewFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
4、在没有使用Spring提供的Open Session In View情况下,因需要在service(or Dao)层里把session关闭,所以lazy loading 为true的话,要在应用层内把关系集合都初始化,如 company.getEmployees(),否则Hibernate抛session already closed Exception;
4、OpenSessionInViewInterceptor 配置
OpenSessionInViewInterceptor和OpenSessionInViewFilter,功能相同,只是一个在web.xml配置,另一个在application.xml配置而已。 <bean name="openSessionInViewInterceptor"
class="org.springframework.orm.hibernate3.support.OpenSessionInViewInterceptor">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<!-- 只是用于springMVC -->
<bean id="urlMapping"
class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="interceptors">
<list>
<ref bean="openSessionInViewInterceptor" />
</list>
</property>
</bean>