Spring 3.2.2 与Hibernate 4.3.5 集成(事务交给Spring管理)

来源:互联网 发布:跟程序员交流用的术语 编辑:程序博客网 时间:2024/05/16 06:42

配置:

<span style="font-family: Arial, Helvetica, sans-serif;"><prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate4.SpringSessionContext</prop></span>

然后用sessionFactory.getCurrentSession()时会出现org.hibernate.HibernateException: No CurrentSessionContext configured!的异常

然后先百度后google,大部分都是说将org.springframework.orm.hibernate4.SpringSessionContext改为thread或将sessionFactory.getCurrentSession()改为sessionFactory.openSession(),但那样做就没法用spring来管理hibernate的事务了,根本不可取

最后以为是事务没配置好,但手动添加事务后仍然报这个错,最后没辙,自己在获取session时用

SpringSessionContext ssc = new SpringSessionContext((SessionFactoryImplementor) this.sessionFactory);Session cs = ssc.currentSession();

初始化一个session,这里不报CurrentSessionContext configured这个错了,但出现新的异常:java.lang.NoClassDefFoundError: org/hibernate/service/jta/platform/spi/JtaPlatform,最后查了下原来hibernate-core-4.3.5.Final.jar中没有jta包,我下载的源码中有jta,jar中竟然没有,有点吭爹,看来官网的东西也不是百分百靠谱,最后把hibernate-core-4.3.5.Final.jar替换成hibernate-core-4.2.15.Final.jar换回原来的方法就正常了

转载请注明出处http://blog.csdn.net/lt_1029/article/details/38084651,谢谢!


0 0
原创粉丝点击