springMVC整合hibernate的时候数据插入需要flush问题
2015-04-11 18:03
423 查看
hibernate中openSession和getCurrentSession
openSession需要手动的管理事务,每次打开一个新的sessiongetCurrentSession在当前线程中找一个session,如果没有则新建一个。不需要手动close,但是需要定义事务管理
spring配合hibernate使用getCurrentSession
由于使用getCurrentSession需要使用事务,spring和hibernate整合的时候使用spring接管hibernate的事务步骤如下:
1.需要在spring的web.xml中配置session open
web.xml加入配置<!-- 配置Session --> <filter> <filter-name>openSession</filter-name> <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class> </filter> <filter-mapping> <filter-name>openSession</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
2.凡是加上事务的bean,必须用xml的方式配置,不能用注解
(事务可以使用注解,测试环境 spring3.2.10+hibernate4.1.9)例如:SecurityService类中有配置事务
application.xml
<bean id="securityService" class="nanhu.lf.service.SecurityService" />
SecurityService类
package nanhu.lf.service; import java.util.List; import nanhu.lf.dao.UserDAO; import nanhu.lf.model.TestObj; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.transaction.annotation.Transactional; public class SecurityService { @Autowired private UserDAO userDAO; @Transactional public void saveUser(TestObj user){ userDAO.saveUser(user); } @Transactional public List<TestObj> getAllUser(){ return userDAO.findAll(); } }
3.配置事务 (注解和xml方式都可以)
1.使用注解方式:<!-- 配置注解管理事务--> <tx:annotation-driven transaction-manager="txManager"/>
2.使用xml方式
<!-- 这是事务通知操作,使用的事务管理器引用自 transactionManager --> <tx:advice id="txAdvice" transaction-manager="txManager"> <tx:attributes> <tx:method name="save*" propagation="REQUIRED" read-only="false"/> <tx:method name="delete*" propagation="REQUIRED" /> <tx:method name="get*" propagation="REQUIRED" read-only="true"/> <tx:method name="query*" propagation="REQUIRED" read-only="true"/> <tx:method name="*" propagation="REQUIRED" /> </tx:attributes> </tx:advice> <!-- 需要引入aop的命名空间 --> <aop:config> <aop:pointcut id="serviceMethods" expression="execution(* nanhu.lf.service.*.*(..))" /> <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethods" /> </aop:config>
事务管理器如下:
<!-- 事物管理器 --> <bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"></property> </bean>
这个也许是使用springMVC之后导致的一个bug吧,或者还有什么其他的配置,网上很多说法,感觉最靠谱的还是 spring容器和springMVC容器之间存在父子关系,那么装配带有事务的bean的时候可能出现重复,所以才会加载的是没有事务的service,具体还不清楚。
在spring的配置文件中,和MVC相关的bean配置在spring-servlet.xml中,其他的放在是spring.xml(application.xml)中。
相关文章推荐
- hibernate先删除数据,紧接着执行插入时的异常解决之道——中间不能调用flush()、clear()等方法
- Spring事务问题 - 大数据clob插入需要事务
- Java_JavaEE_SSH_hibernate向mysql插入数据引起中文乱码问题
- SpringMVC-Hibernate关于数据更新没有持久化的问题
- springMvc整合hibernate出现的问题
- 数据整合需要注意的问题
- 用Maven整合SpringMVC+Spring+Hibernate 框架,实现简单的插入数据库数据功能
- mysql 插入string类型变量时候,需要注意的问题,妈的,害我想了好几个小时!!
- SSH整合时候Hibernate正确执行了save方法,但是数据没有保存到数据库
- Spring、Hibernate 数据不能插入到数据库问题解决
- Struts Spring Hibernate 框架整合时候出现的问题
- Hibernate save 插入数据问题
- 当需要为一个既存的类中增加一个数据成员的时候,你会想到那些问题?
- hibernate保存数据后在修改的时候结果是老数据的问题
- eclipse3.2下解决数据插入mysql数据库时乱码无法插入问题(Hibernate)
- Struts Spring Hibernate 框架整合时候出现的问题
- 在定义SharePoint列表的SPD数据视图的时候需要注意的问题
- 在写springmvc整合hibernate的时候报警告No mapping found for HTTP request with URI的原因
- 用hibernate向mysql插入数据时问号乱码的问题解决!!
- springMvc整合hibernate出现问题