自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Hibernate Callback簡單概括

開發(fā) 后端
這里介紹使用HibernateTemplate執(zhí)行execute(new Hibernate Callback())方法,從Hibernate Callback中得到session,在此session中做多個操作,并希望這些操作位于同一個事務(wù)中。

本文向大家介紹Hibernate Callback,可能好多人還不了解Hibernate Callback,沒有關(guān)系,看完本文你肯定有不少收獲,希望本文能教會你更多東西。

目的:使用HibernateTemplate執(zhí)行execute(new Hibernate Callback())方法,從Hibernate Callback中得到session,在此session中做多個操作,并希望這些操作位于同一個事務(wù)中。

如果你這樣寫:

  1. public static void main(String ss[]) {  
  2. CtxUtil.getBaseManager().getHibernateTemplate().execute(new HibernateCallback() {  
  3. public Object doInHibernate(Session session) throws HibernateException, SQLException {  
  4. // 保存stu1  
  5. Student stu1 = new Student();  
  6. stu1.setName("aaaa");// 在數(shù)據(jù)庫中,name字段不允許為null  
  7. session.save(stu1);  
  8. session.flush();  
  9. //實際上,如果不是程序員"手癢"來調(diào)用這個flush(),HibernateTemplate中session的事務(wù)處理還是很方便的  
  10.  
  11. Student stu2 = new Student();  
  12. session.save(stu2);  
  13. // 沒有設(shè)置name字段,預(yù)期會報出例外  
  14. session.flush();  
  15. return null;  
  16. }  
  17. }

你期望spring在執(zhí)行完execute回調(diào)后,在關(guān)閉session的時候提交事務(wù),想法是很好的,但spring并不會這么做。讓我們來看看在Hibernate的源代碼中,session.beginTransation()做了什么事??慈缦麓a:

  1. public Transaction beginTransaction() throws HibernateException {  
  2. errorIfClosed();  
  3. if ( rootSession != null ) {  
  4. // todo : should seriously consider not allowing a txn to begin from a child session  
  5. //can always route the request to the root session  
  6. log.warn( "Transaction started on non-root session" );  
  7. }  
  8. Transaction result = getTransaction();  
  9. result.begin();  
  10. return result;  

這個方法中的result是一個org.hibernate.transaction.JDBCTransaction實例,而方法中的getTransaction()方法源代碼為:

  1. public Transaction getTransaction() throws HibernateException {  
  2. if (hibernateTransaction==null) {  
  3. log.error(owner.getFactory().getSettings().getTransactionFactory().getClass());  
  4. hibernateTransaction = owner.getFactory().getSettings().
    getTransactionFactory().createTransaction( this, owner );  
  5. }  
  6. return hibernateTransaction;  

再次追蹤,owner.getFactory()。getSettings() .getTransactionFactory()的createTransaction()方法源代碼如下:

  1. public Transaction createTransaction
    (JDBCContext jdbcContext, Context transactionContext)  
  2. throws HibernateException {  
  3. return new JDBCTransaction( jdbcContext, transactionContext );  

它返回了一個JDBCTransaction,沒什么特別的。以上介紹Hibernate Callback方法。

【編輯推薦】

  1. Hibernate專業(yè)知識介紹
  2. 討論Hibernate和模型對象
  3. Hibernate查詢緩存全面分析
  4. 概括Hibernate主鍵生成機制
  5. Hibernate Pager基礎(chǔ)介紹
責任編輯:佚名 來源: blogjava
相關(guān)推薦

2009-09-27 14:33:01

Hibernate批量

2009-09-21 16:56:14

Hibernateibatis

2009-09-24 09:25:10

Hibernate批量

2009-09-25 15:15:54

Hibernate檢索

2009-09-28 15:24:38

Hibernate V

2009-09-28 17:27:27

Hibernate A

2009-09-22 17:55:51

Spring Hibe

2009-09-29 16:29:40

Hibernate查詢

2009-09-23 17:28:55

Hibernate C

2009-09-22 09:31:15

Hibernate主鍵

2009-09-28 09:56:53

Hibernate屬性

2009-09-22 09:40:03

cascade和invHibernate

2009-09-22 13:12:25

Hibernateibatis

2009-09-23 18:05:48

2009-09-25 13:18:15

Hibernate數(shù)據(jù)

2009-09-21 16:40:42

Hibernate可行

2009-09-22 14:52:55

Hibernate p

2009-09-23 13:21:32

Hibernate O

2009-09-24 13:45:53

Hibernate性能

2009-09-25 10:22:35

Hibernate多表
點贊
收藏

51CTO技術(shù)棧公眾號