1. 程式人生 > >解決load get save 等is not valid without active transaction異常的原

解決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 標籤:
休閒
is not valid without active 生活 Hibernate 職場

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

這裡寫圖片描述