operations are not allowed in read-only mode
org.springframework.dao.InvalidDataAccessApiUsag
1.spring的配置文件一定要写,找到applicationContext-core.xml并打开,其中<tx:/>标签很重要,可能需手动配
2.spring中的包的路径一定要写对,下面的红色部分不能写错。
在用openSessionInView时,如果采用默认的事务管理(就是在spring配置文件里没有配置事务管理),在调用HibernateDaoSupport类里的getHibernateTemplate()里的增删改方法时,会抛出org.springframework.dao.InvalidDataAccessApiUsag
第一次看到这个错,肯定会觉得纳闷,它要你修改session的模式,或者移除readOnly marker.但是你从来都没有设置session的模式.
原因:spring2.0里面的OpenSessionInViewFilter的getSession方法中会对session的flushMode设定一个默认为NEVER的值,而这个值在hibernate3.0似乎是不能理解的,当然就不行了,所以报错就要你修改session的模式.
解决方法:
方法一.,就是继承OpenSessionInViewFilter类,然后重写这个方法,加句 this.setFlushMode(FlushMode.AUTO);或者干脆把session里面直接扔个FlushMode.AUTO,然后再重写一个叫closeSession的方法,记住一定要重写,因为增加了flushMode以后要调用session.flush()才可以正常提交数据,其实重写closeSession就是为了加1句session.flush(),然后下面调用super.closeSession()方法就行了。这种方法是我在网上找的一网友的解决方法,不过我不推荐这种方法
方法二:在spring配置文件里配置自己的事务,
</bean>
<tx:advice >
</tx:advice>
<aop:config>
</aop:config>
(转自http://blog.163.com/littlefermat/blog/static/5977116720096281075177/)
浅谈Struts+Hibernate+Spring的整合方法
1、在工程中导入spring支持,导入的Jar包有:
◆
◆Spring
◆
◆
2、在Spring配置文件中配置dataSource和S
1、在工程中导入spring支持,导入的Jar包有:
◆ Spring 2.0 Core Libraries
◆Spring 2.0 ORM/DAO/Hibernate3 Libraries
◆ Spring 2.0 AOP Libraries
◆ Spring 2.0 Web Libraries
2、在Spring配置文件中配置dataSource和SessionFactory,将hibernate配置与Spring配置整合在一起(可以删除hibernate.cfg.xml文件);
3、导入数据库源所要使用的Jar包,如:DBCP所用的JAR包(commons-pool.jar);
4、修改所有DAO的hibernate实现,因为Spring中提供了一个HibernateDAOSupport类,可以简化数据库的操作。使用所有DAO类都继承自该类;
5、将DAO采用依赖注入的方式注入到Biz中,再将Biz采用依赖注入的方式注入到Action中,在Spring配置文件中做相应配置;
6、将Spring与Struts集成:
1)在spring配置文件配置Action:将Biz注入到Action中;
2)修改Struts的配置文件:将Action的type属性修改为:org.springframework.web.struts.DelegatingActionProxy;
3)在web.xml文件中配置监听器以及web应用的初始化参数:
contextConfigLocation /WEB-INF/applicationContext.xml /WEB-INF/applicationContext-beans.xml org.springframework.web.context.ContextLoaderListener |
7、为了解决应用中的中文乱码问题,我们可以不用自己开发过滤器类,Spring为我们提供了一个,只需要配置一下即可:
characterEncodingFilter org.springframework.web.filter.Charact [color=brown][/color]erEncodingFilter encoding UTF-8 characterEncodingFilter /* |
8、为了解决hibernate延迟加载的问题,使用Spring中提供的过滤器来解决,它能够让Session
在请求解释完成之后再关闭,配置方式如下:
hibernate session manager filter org.springframework.orm.hibernate3.support. OpenSessionInViewFilter hibernate session manager filter /* |
9、因为OpenSessionInViewFilter在getSession的时候,会把获取回来的session的flush mode 设为FlushMode.NEVER。故进行insert、 update和delete操作时会产生异常:org.springframework.dao.InvalidDataAccessApiUsag
<!-- 配置事务管理器 --> <bean id="transactionManager" class=" org.springframework.orm.hibernate3.HibernateTransactionMana |
10、 部署应用程序,启动服务器,如果发现异常: java.lang.NoSuchMethodError: org.objectweb.asm.ClassVisitor.visit(IILjava/lang/String;Ljava/lang/String;[Ljava/lang/String;Ljava/lang/String;)V 这是由于整合时Jar包的冲突引起的。应将"Web应用程序/WEB-INF/lib/asm-2.2.3.jar"删除即可