springmvc3+hibernate3整合问题
来源:互联网 发布:apk源码反编译工具 编辑:程序博客网 时间:2024/04/29 03:43
今天做ssh整合删除时,发现hibernate在删除对象时,不执行sql,也不打印sql语句。所以就查了下,整理以便自己学习。。
一、问题
springmvc+hibernate只执行保存操作,不能更新和删除。网上都说是事物控制的问题,最后我发现一句话说的有道理:SpringMVC的配置文件中,只能扫描Controller的注解,不然Spring对hibernate的事务控制将会失效。
怎么会这样呢?
1.对于用annotation方式的事务注解和bean配置,spring的配置文件 与springMVC的配置文件对包的重复扫描装配会造成失效
在主容器中(applicationContext.xml),将Controller的注解排除掉
<context:component-scan base-package="com">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
</context:component-scan>
而在springMVC配置文件中将Service注解给去掉
<context:component-scan base-package="com">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service" />
</context:component-scan>
<context:component-scan base-package="com">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service" />
</context:component-scan>
因为spring的context是父子容器,所以会产生冲突,由ServletContextListener产生的是父容器,springMVC产生的是子容器,子容器Controller进行扫描装配时装配了@Service注解的实例,而该实例理应由父容器进行初始化以保证事务的增强处理,所以此时得到的将是原样的Service(没有经过事务加强处理,故而没有事务处理能力。)
二、session的获取和比较
1、getCurrentSession()与openSession()的区别
* 采用getCurrentSession()创建的session会绑定到当前线程中,而采用openSession()
创建的session则不会
* 采用getCurrentSession()创建的session在commit或rollback时会自动关闭,而采用openSession()创建的session必须手动关闭
2、使用getCurrentSession()需要在hibernate.cfg.xml文件中加入如下配置:
* 如果使用的是本地事务(jdbc事务)
<property name="hibernate.current_session_context_class">thread</property>
* 如果使用的是全局事务(jta事务)
<property name="hibernate.current_session_context_class">jta</property>
三、解决方法
* 采用getCurrentSession()创建的session会绑定到当前线程中,而采用openSession()
创建的session则不会
* 采用getCurrentSession()创建的session在commit或rollback时会自动关闭,而采用openSession()创建的session必须手动关闭
2、使用getCurrentSession()需要在hibernate.cfg.xml文件中加入如下配置:
* 如果使用的是本地事务(jdbc事务)
<property name="hibernate.current_session_context_class">thread</property>
* 如果使用的是全局事务(jta事务)
<property name="hibernate.current_session_context_class">jta</property>
三、解决方法
最后我采用了session.flush();在delete执行后加上刷新就可以执行了。
如果哪位大神有更好的方法,可以留言,谢谢。。。。
0 0
- springmvc3+hibernate3整合问题
- springMVC3.2+spring3.2.1+hibernate3.2整合
- Spring2整合Hibernate3.1问题
- MyBatis3整合Spring3、SpringMVC3
- MyBatis3整合Spring3、SpringMVC3
- MyBatis3整合Spring3、SpringMVC3
- MyBatis3整合Spring3、SpringMVC3
- MyBatis3整合Spring3、SpringMVC3
- MyBatis3整合Spring3、SpringMVC3
- MyBatis3整合Spring3、SpringMVC3
- MyBatis3整合Spring3、SpringMVC3
- SpringMVC3 + SpringJDBC整合
- SpringMVC3 + SpringJDBC整合
- SpringMVC3 + SpringJDBC整合
- MyBatis3整合Spring3、SpringMVC3
- MyBatis3整合Spring3、SpringMVC3
- MyBatis3整合Spring3、SpringMVC3
- MyBatis3整合Spring3、SpringMVC3
- mmm 主从复制机制排错过程
- 售前
- forward内部跳转 和redirect重定向跳转的区别与共同点
- 《设计模式》之Creational模式:Builder
- 深入浅出ClassLoader
- springmvc3+hibernate3整合问题
- Android中Recyclerview使用6----添加条目得到点击事件和长按事件(另一种写法,较简单)
- 121. Best Time to Buy and Sell Stock
- 你应该知道的16个Linux服务器监控命令
- valid Palindrome -- leetcode
- 八大排序算法
- leetcode题解-205.Isomorphic Strings && 290. Word Pattern
- memcache搜索分页
- native app 集成 cocos-2dx-js 3.11 (Android篇)