基礎組態檔
檔案名:hibernate.cfg.xml
檔案位置:src目錄下

核心檔案內容說明
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!--鏈接資料庫基礎 必須--> <property name="connection.driver_class">com.mysql.cj.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql://localhost/hibernate?useSSL = FALSE & serverTimezone = UTC</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">root</property> <!--資料庫方言 必須--> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <!--資料庫默認模式 修改(無表結構則新增表結構,有就修改)--> <property name="hbm2ddl.auto">update</property> <!--顯示sql陳述句--> <property name="show_sql">true</property> <!--sql陳述句格式化--> <property name="format_sql">true</property> <!--將session系結到當前執行緒中,獲得getCurrentSession--> <property name="hibernate.current_session_context_class">thread</property> <!--加載組態檔 必須--> <mapping resource="com/hibernate/entity/User.hbm.xml" ></mapping> </session-factory> </hibernate-configuration>
易錯:NOT_ACTIVE
錯誤提示:
1.org.hibernate.HibernateException: Calling method 'save' is not valid without an active transaction (Current status: NOT_ACTIVE)
2.org.hibernate.HibernateException: createSQLQuery is not valid without active transaction
原因:獲取到的session不是同一個
代碼:
工具類(HibernateUtils):
package com.hibernate.utils; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; public class HibernateUtils { private static final Configuration CONFIGURATION; private static final SessionFactory SESSION_FACTORY; static { CONFIGURATION = new Configuration(); CONFIGURATION.configure(); SESSION_FACTORY = CONFIGURATION.buildSessionFactory(); } //單獨開啟一個session public static Session getSession(){ return SESSION_FACTORY.openSession(); } //使用getCurrentSession獲取的session是系結在執行緒上的 public static Session getCurrentSession(){ return SESSION_FACTORY.getCurrentSession(); } }
注意:上面有兩個獲取session的方法
1.直接開啟一個session (getSession())
2.從執行緒中獲取一個session (getCurrentSession())
<!--將session系結到當前執行緒中,獲得getCurrentSession--> <property name="hibernate.current_session_context_class"> thread </property>
service層類(UserServiceImpl):
package com.hibernate.service.impl; import com.hibernate.dao.UserDao; import com.hibernate.entity.User; import com.hibernate.service.UserService; import com.hibernate.utils.BeanFactory; import com.hibernate.utils.HibernateUtils; import org.hibernate.Session; import org.hibernate.Transaction; public class UserServiceImpl implements UserService { @Override public void addUser() { //開啟事務 Session session = HibernateUtils.getSession(); Transaction tr = session.beginTransaction(); try { //開始添加用戶 UserDao dao = (UserDao) BeanFactory.getBeanFactory("UserDao"); User user = new User("胖子", 18); dao.add(user); int i = 10/ 0; User user2 = new User("哈哈", 18); dao.add(user2); tr.commit(); } catch (Exception e) { e.printStackTrace(); tr.rollback(); } } }
這里session獲取是用的getSession(),
也就是重新開啟的一個session,
并沒有從執行緒中去獲取
Dao層的類(UserDaoImpl):
package com.hibernate.dao.impl; import com.hibernate.dao.UserDao; import com.hibernate.entity.User; import com.hibernate.utils.HibernateUtils; import org.hibernate.Session; public class UserDaoImpl implements UserDao { @Override public void add(User user) { Session cs = HibernateUtils.getCurrentSession(); cs.save(user); } }
這里獲取session使用的是getCurrentSession()
獲取的是執行緒中的session,
所以和上面service層的session不是同一個
提示:
在spring框架中會幫我們代理這個session,所以我們不需要配置
<property name="hibernate.current_session_context_class"> thread </property>
否則會報第二個錯誤
網址:https://blog.csdn.net/yinjian520/article/details/8666695
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/226420.html
標籤:Java
