解决load get save 等is not valid without active transaction异常的原

分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow

也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!

                org.hibernate.HibernateException:*** is not valid without active transaction 2010-12-09 15:36:07

Exception in thread "main" org.hibernate.HibernateException: *** is not valid without active transaction
 at org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:338)
 at $Proxy13.getListeners(Unknown Source)
 at org.hibernate.search.backend.impl.EventSourceTransactionContext.getIndexWorkFlushEventListener(EventSourceTransactionContext.java:78)
 at org.hibernate.search.backend.impl.EventSourceTransactionContext.<init>(EventSourceTransactionContext.java:41)
 at org.hibernate.search.impl.FullTextSessionImpl.<init>(FullTextSessionImpl.java:75)
 at org.hibernate.search.Search.getFullTextSession(Search.java:23)
 at com.yin.hibernate.model.GuestBookTest.main(GuestBookTest.java:28)

在网上找的原因是:
如果使用hibernate的getCurrentSession()获得session对象,对查询语句也需要开启事务,不然就会抛出如上异常!
解决方法

加上  Transaction tx = session.beginTransaction(); 
      tx.commit();
注意:在 hibernate.cfg.xml
如果采用jdbc独立引用程序配置如下:
<property name=”hibernate.current_session_context_class”>thread</property>
如果采用了JTA事务配置如下
<property name=”hibernate.current_session_context_class”>jta</property>

           

给我老师的人工智能教程打call!http://blog.csdn.net/jiangjunshow

这里写图片描述

猜你喜欢

转载自blog.csdn.net/jdtugfcg/article/details/84000653