1. 程式人生 > >spring整合hibernate的時候使用session的時候報:is not valid without active transaction

spring整合hibernate的時候使用session的時候報:is not valid without active transaction

在使用spring整合hibernate的,然後測試環境的時候報錯:org.hibernate.HibernateException: save is not valid without active transaction

我使用的環境是spring4.3.*和hibernate5版本。以下是示例程式碼:

這裡寫圖片描述

這裡寫圖片描述

此時執行測試會報:org.hibernate.HibernateException: save is not valid without active transaction這個錯誤
網上查了一下說是spring高版本在使用事務的時候需要在spring配置檔案中配置hibernate.current_session_context_class的屬性值為org.springframework.orm.hibernate5.SpringSessionContext

改過來之後又報這個錯誤:org.hibernate.HibernateException: Could not obtain transaction-synchronized Session for current thread;
然後解決方法就是在spring中配置事務管理然後在相關類中注入事務就搞定了。
參考文件:
Hibernate4 No Session found for current thread原因
spring,hiberante之* is not valid without active transaction