`
woaiyingyu123
  • 浏览: 69714 次
  • 性别: Icon_minigender_1
  • 来自: 广西
社区版块
存档分类
最新评论

Spring管理Hibernate的延迟加载配置(OpenSessionInViewFilter)

阅读更多
当我们使用延迟加载,但是如果用Spring管理Hibernate,那么每次操作都会关闭Session。这样触发延迟加载就会导致session已经关闭的错误。但是如果我们不使用延迟加载,那么表的关联关系一旦很庞大,数据很多。那么效率也是个不可忽视的问题。进入正题。
(1)web.xml的配置
<?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>
	<!-- Spring管理延迟加载的配置 -->//这个顺序很重要,放在Struts2的配置的上方,这个小细节困扰了我很久。。。不知道是否正确,反正我实验成功
	<filter>
		<filter-name>hibernateFilter</filter-name>
		<filter-class>
			org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
		<init-param>
			<param-name>sessionFactoryBeanName</param-name>
			<param-value>sessionFactory</param-value>//sessionFactory的Bean的名字
		</init-param>
		<init-param>
			<param-name>singleSession</param-name>
			<param-value>true</param-value>//默认为true,如果为false,等于没用OpenSessionInView,默认为true,所以这个初始化参数可以省略的
		</init-param>
		<init-param>
			<param-name>flushMode</param-name>
			<param-value>AUTO</param-value>//设为自动
		</init-param>

	</filter>
	<filter-mapping>
		<filter-name>hibernateFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	<!-- Struts2的配置 -->
	<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>
	<!-- Spring管理配置 -->
	<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>
//在这里简单介绍flushMode的几个属性:
 (1)NEVEL,已经废弃了,被MANUAL取代了
 (2)MANUAL,spring3.x中的opensessioninviewfilter已经将默认的FlushMode设置为MANUAL了;如果FlushMode是MANUAL或NEVEL,在操作过程中hibernate会将事务设置为readonly,所以在增加、删除或修改操作过程中会出现如下错误
(3)AUTO,设置成auto之后,当程序进行查询、提交事务或者调用session.flush()的时候,都会使缓存和数据库进行同步,也就是刷新数据库
(4)COMMIT,提交事务或者session.flush()时,刷新数据库;查询不刷新
(5)每次进行查询、提交事务、session.flush()的时候都会刷数据库
这里需要说一下和AUTO的区别,当hibernate缓存中的对象被改动之后,会被标记为脏数据(即与数据库不同步了)。当session设置为FlushMode.AUTO时,hibernate在进行查询的时候会判断缓存中的数据是否为脏数据,是则刷数据库,不是则不刷,而always是直接刷新,不进行任何判断。很显然auto比always要高效得多。


(2)spring的sessionFactory配置:(applicationContext.xml)
<bean id="sessionFactory"
		class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
		<property name="dataSource">
			<ref bean="bbsDateSource" />
		</property>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
				<prop key="hibernate.hbm2ddl.auto">update</prop>
				<prop key="hibernate.show_sql">true</prop>
			</props>
		</property>
		<property name="packagesToScan">
			<value>com.xxx.entity</value>
		</property>
	</bean>
//这个配置在Spring配置的文章上有了介绍,请参照。

如果有什么不足的请留言,共同学习,共同进步!还有一点,需要加上Spring事务,这样才能刷新(这个只是我个人遇到的问题,这里说一下给大家,不知道是否正确,否则改天弄来弄去还是不成功,那就郁闷啦。呵呵~)。
分享到:
评论

相关推荐

    struts+spring+hibernate整合

    Spring4.0、Struts2.3.15、Hibernate4.2.4、jQuery1.9.1涉及到了诸多开发时的细节:ModelDriven、Preparable 拦截器、编写自定义的类型转换器、Struts2 处理 Ajax、OpenSessionInViewFilter、迫切左外连接、Spring ...

    OpenSessionInViewFilter

    OpenSessionInViewFilter个人学习总结

    jar包(struts2.0.8+spring2.0+hibernate3.2)

    struts2.0.8+spring2.0+hibernate3.2 jar包

    spring_demo:Spring MVC示范项目

    Spring MVC Hibernate Demo Hibernate 配置 数据库实体必须设置以下注解 @Entity ... &lt;filter&gt;org.springframework.orm.hibernate4.support.OpenSessionInViewFilter &lt;param&gt;flushMode&lt;/param-nam

    关于OpenSessionInViewFilter的学习

    NULL 博文链接:https://yanzhenwei.iteye.com/blog/1701164

    spring_note.rar_inversion_spring concept

    课程内容 面向接口(抽象)编程的概念与好处 IOC/DI的概念与好处 ...Struts2.1.6 + Spring2.5.6 + Hibernate3.3.2整合(重要) opensessionInviewfilter(记住,解决什么问题,怎么解决) Spring JDBC

    OA项目SSH整合框架

    &lt;filter-class&gt;org.springframework.orm.hibernate3.support.OpenSessionInViewFilter &lt;filter-name&gt;OpenSessionInView *.do 2,LazyInitializationException异常说明 1,对于集合属性...

    SPRING API 2.0.CHM

    OpenSessionInViewFilter OpenSessionInViewFilter OpenSessionInViewInterceptor OpenSessionInViewInterceptor OptimisticLockingFailureException OptionsTag OptionTag OptionWriter OracleLobHandler ...

    SSH项目整合示例【OpenSessionInView】所用到的jar包

    SSH项目整合示例【OpenSessionInView】所用到的jar包 包含Struts + Hibernate + Spring所有jar及其依赖的jar

    Sping 事务管理.doc

    OpenSessionInViewFilter解决Web应用程序的问题

    S2SH集成 案例

    该案例实现了一个简单的登录功能,但里面将S2SH集成的所有配置信息都添加进去了。 如,OpenSessionInViewFilter、声明式事务、三层等等

    Mac Mysql数据库中文乱码问题解决

    如:在使用Java中得SSH框架时,我们需要在web.xml文件中配置编码的filter,具体代码是: &lt;span xss=removed&gt;&lt;!-- 表单处理乱码,必须在OpenSessionInViewFilter的filter之前 --&gt; &lt;filter&gt;CharacterFilter ...

Global site tag (gtag.js) - Google Analytics