標籤:關聯式資料庫 service contex 資料 csdn 自己 int open mod
1. Hibernate經常使用配置
使用hibernate.default_schema屬效能夠讓全部產生的表都帶一個指定的首碼。
2. session簡單介紹
不建議直接使用jdbc的connection操作資料庫,而是通過使用session操作資料庫。
session能夠理解為操作資料庫的對象。session與connection是多對一的關係。每一個session都有一個與之相應的connection。一個connection不同一時候刻能夠供多個session使用。把對象儲存在關聯式資料庫中須要調用session的各種方法,如save(),update(),delete(),createQuery()等。
3. transaction簡單介紹
hibernate對資料的操作都是封裝在事務其中,而且預設是非自己主動提交的方式。所以用session儲存對象時。假設不開啟事務,而且手工提交事務。對象並不會真正儲存在資料庫中。
假設想讓hibernate像jdbc那樣自己主動提交事務,必須調用session對象的doWork()方法。獲得jdbc的connection後。設定其為自己主動提交事務模式。
比如:
session.doWork(new Work() {@Overridepublic void execute(Connection conn) throws SQLException {conn.setAutoCommit(true);}});session.save(s1); // 儲存對象進入資料庫session.flush();
注意一定不能忘記flush()方法。
4. session具體解釋
擷取session對象的兩種方式:
(1). SessionFactory的openSession()方法
(2) SessionFactory的getCurrentSession()方法
假設使用getCurrentSession須要在hibernate.cfg.xml檔案裡進行配置:
假設是本地事務(jdbc事務)
<property name="hibernate.current_session_context_class">thread</property>
假設是全域事務(jta事務)
<property name="hibernate.current_session_context_class">jta</property>
openSession和getCurrentSession的差別:
(1). getCurrentSession在事務提交或者復原之後會自己主動關閉,而openSession須要你手動關閉。假設使用openSession而沒有手動關閉。多次之後就會導致串連池溢出。
(2). openSession每次建立新的session對象,而getCurrentSession使用現有的session對象。
@Testpublic void testSaveStudentWithOpenSession() {// 獲得設定物件Configuration config = new Configuration().configure();// 獲得服務注冊對象ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry();// 獲得SessionFactory對象SessionFactory sessionFactory = config.buildSessionFactory(serviceRegistry);// 獲得Session對象Session session1 = sessionFactory.openSession();// 開啟事物Transaction transaction = session1.beginTransaction();// 產生一個學生對象Student s = new Student(1, "李四", "男", new Date(), "北京");session1.doWork(new Work() {@Overridepublic void execute(Connection conn) throws SQLException {System.out.println("Connection的hashCode:" + conn.hashCode());}});session1.save(s);// session1.close();// 提交事務transaction.commit();// 新建立一個session提交Session session2 = sessionFactory.openSession();transaction = session2.beginTransaction();s = new Student(2, "王五", "男", new Date(), "上海");session2.doWork(new Work() {@Overridepublic void execute(Connection conn) throws SQLException {System.out.println("Connection的hashCode:" + conn.hashCode());}});session2.save(s);transaction.commit();}@Testpublic void testSaveStudentWithGetCurrentSession() {// 獲得設定物件Configuration config = new Configuration().configure();// 獲得服務注冊對象ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry();// 獲得SessionFactory對象SessionFactory sessionFactory = config.buildSessionFactory(serviceRegistry);// 獲得Session對象Session session1 = sessionFactory.getCurrentSession();// 開啟事物Transaction transaction = session1.beginTransaction();// 產生一個學生對象Student s = new Student(1, "李四", "男", new Date(), "北京");session1.doWork(new Work() {@Overridepublic void execute(Connection conn) throws SQLException {System.out.println("Connection的hashCode:" + conn.hashCode());}});session1.save(s);// session1.close();// 提交事務transaction.commit();// 新建立一個session提交Session session2 = sessionFactory.getCurrentSession();transaction = session2.beginTransaction();s = new Student(2, "王五", "男", new Date(), "上海");session2.doWork(new Work() {@Overridepublic void execute(Connection conn) throws SQLException {System.out.println("Connection的hashCode:" + conn.hashCode());}});session2.save(s);transaction.commit();}
通過範例我們能夠看出在不關閉session的情況下,使用openSession每一次獲得的都是新的session,而使用getCurrentSession獲得的都是同一個session,所以在使用openSession獲得的session操作完成後。須要手動close掉。
5. hbm設定檔經常使用設定<hibernate-mapping>標籤經常使用屬性:
<class>標籤經常使用屬性:
<id>標籤的經常使用屬性:
常見主鍵建置原則:
【Hibernate】(2)Hibernate配置與session、transaction