[color=green][/color]
在保存实体对象的时候出现一下错误
错误提示:Write operations are not allowed in read-only mode (FlushMode.NEVER) - turn your Session into FlushMode.AUTO or remove 'readOnly' marker from transaction definition
网上速查是使用OpenSessionInViewFilter的缘故
OpenSessionInViewFilter在web.xml中的配置如下:
<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>/*</url-pattern>
</filter-mapping>
首先说明OpenSessionInViewFilter的作用是:允许在每次的整个request的过程中使用同一个hibernate session,可以在这个request任何时期延迟加载数据(hibernate延迟加载)。即实现了spring对session的管理,又实现了hibernate的延迟加载,一句两得的事情,多好
!
但是OpenSessionInViewFilter默认是不会对session 进行flush的,并且flush mode 是 never
代码:
protected Session getSession(SessionFactory sessionFactory)
throws DataAccessResourceFailureException {
Session session = SessionFactoryUtils.getSession (sessionFactory, true);
session.setFlushMode(FlushMode.NEVER);
return session;
}
看getSession的方式就知道,把flush mode 设为FlushMode.NEVER,这样就算是commit的时候也不会session flush, 如果想在完成request过程中更新数据的话, 那就需要先把flush model设为FlushMode.AUTO,再在更新完数据后flush.
解决方法:使用spring的事物管理机制(spring2.0及以后版本)
<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="sessionFactory"/>
</property>
</bean>
<!-- 配置事务特性 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED"/>
<tx:method name="del*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="*" read-only="true"/>
</tx:attributes>
</tx:advice>
<!-- 配置那些类的方法进行事务管理 -->
<aop:config>
<aop:pointcut id="allManagerMethod"
expression="execution (* com.cyf.oa.managers.*.*(..))"/>
<aop:advisor advice-ref="txAdvice"
pointcut-ref="allManagerMethod"/>
</aop:config>
还有一点就是方法名的问题,方法名字一定要与事物配置声明的方法名对应,一定要以add,del或者update开头,要不然这些配置对你那方法是不起作用的。、
这些都是spring,hibernate对我们的方法进行了控制,在调用某种方法前,进行了验证,过滤。帮助我们管理事物。
分享到:
相关推荐
NULL 博文链接:https://yanzhenwei.iteye.com/blog/1701164
SSH项目整合示例【OpenSessionInView】所用到的jar包 包含Struts + Hibernate + Spring所有jar及其依赖的jar
struts2.0.8+spring2.0+hibernate3.2 jar包
OpenSessionInViewFilter OpenSessionInViewInterceptor OpenSessionInViewInterceptor OptimisticLockingFailureException OptionsTag OptionTag OptionWriter OracleLobHandler OracleLobHandler....
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter <filter-name>OpenSessionInView *.do 2,LazyInitializationException异常说明 1,对于集合属性...
OpenSessionInViewFilter解决Web应用程序的问题
Spring MVC Hibernate Demo Hibernate 配置 数据库实体必须设置以下注解 @Entity ... <filter>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter <param>flushMode</param-nam
该案例实现了一个简单的登录功能,但里面将S2SH集成的所有配置信息都添加进去了。 如,OpenSessionInViewFilter、声明式事务、三层等等
课程内容 面向接口(抽象)编程的概念与好处 IOC/DI的概念与好处 inversion of control dependency injection AOP的概念与好处 ...opensessionInviewfilter(记住,解决什么问题,怎么解决) Spring JDBC
Spring4.0、Struts2.3.15、Hibernate4.2.4、jQuery1.9.1涉及到了诸多开发时的细节:ModelDriven、Preparable 拦截器、编写自定义的类型转换器、Struts2 处理 Ajax、OpenSessionInViewFilter、迫切左外连接、Spring ...
Mac 下MySQL数据库中文乱码解决方案: 当我们用框架进行数据库的存储操作时,经常会遇到中文乱码...-- 表单处理乱码,必须在OpenSessionInViewFilter的filter之前 --> <filter>CharacterFilter <filter>org.sprin