您的位置:首页 > 编程语言 > Java开发

Spring 3.x 与Hibernate 4.x 整合遇到的问题

2016-09-18 10:35 337 查看

对于提示***
is not valid without active transaction 的错误

可以在Hibernate的配置文件中做如下修改

<prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate4.SpringSessionContext</prop>(Hibernate4)
对于Hibernate3.x,可以直接把上述设置删除,就不会报错了,具体原因尚不清楚。
须知:几乎所有正常的操作都必须在transcation.isActive()条件下才能执行。get,load,save, saveOrUpdate,list都属于这类。
来自:http://blog.csdn.net/wzk527/article/details/8543480






Spring 3.x 与Hibernate 4.x 整合遇到的问题,描述如下:

对数据库的 增加、删除、修改 操作需要Spring的事务支持,所以对service层的上述操作增加事务。applicationContext.xml配置如下:

<tx:advice id="txAdvice" transaction-manager="transactionManager"> 

<tx:attributes> 

<tx:method name="add*" propagation="REQUIRED" read-only="false"/>

<tx:method name="save*" propagation="REQUIRED" read-only="false" />

<tx:method name="remove*" propagation="REQUIRED" read-only="false" />

<tx:method name="delete*" propagation="REQUIRED" read-only="false"/>

<tx:method name="modify*" propagation="REQUIRED" read-only="false"/>

<tx:method name="update*" propagation="REQUIRED" read-only="false" />

<tx:method name="*" propagation="SUPPORTS" read-only="true" /> 

</tx:attributes> 

</tx:advice>

由于Spring 3.x 对 Hibernate 4.x 不提供 HibernateDaoSupport,所以在dao的实现层注入SessionFactory,从而通过

Session session = sessionFactory.getCurrentSession();

来获得当前的session。applicationContext.xm中 sessionFactory的HibernateProperties增加以下属性:

<prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate4.SpringSessionContext</prop>

注意:和Spring2.x不同,不能为thread,否则报错:org.hibernate.HibernateException: save is not valid without active transaction

OK,以上配置在 增加、删除、修改 操作时,都能正确执行,事务也正常执行!

当执行 查询 操作时,不需要事务的支持,代码如下:

public List<Student> getStudentList(String str)

{

  Session session = sessionFactory.getCurrentSession();

  List<Student> list = null;

  list = session.createQuery(str).list();

  return list;

}

问题来了,报错:org.hibernate.HibernateException: No Session found for current thread

问题解决:几乎所有正常的操作都必须在transcation.isActive()条件下才能执行。get,load,save, saveOrUpdate,list都属于这类!

详情可以查看源码!

建议:当方法不需要事务支持的时候,使用 Session session = sessionFactory.openSession()来获得Session对象,问题解决!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: