隨筆-69  評(píng)論-0  文章-5  trackbacks-0
          去年rails正火的時(shí)候,仿active record用hibernate實(shí)現(xiàn)了一個(gè)BasePo,當(dāng)時(shí)用的是一個(gè)
          靜態(tài)sessionfactory注入的方式,感覺(jué)很不好,當(dāng)明也沒(méi)想到好的方案,后來(lái)發(fā)現(xiàn)有人通過(guò)
          threadlocal實(shí)現(xiàn)了下面這個(gè),可以看看:
           1 public class Persistent implements Lifecycle, Validatable, Serializable {
           2    
           3    protected Serializable _id;
           4    protected int _version;
           5 
           6    public Serializable getIdentifier() {
           7       return _id;
           8    }
           9    public void setIdentifier(Serializable id) {
          10       _id = id;
          11    }
          12    public int getVersion() {
          13       return _version;
          14    }
          15    public void setVersion(int version) {
          16       _version = version;
          17    }
          18 
          19    public Long persist() throws HibernateException, SQLException {
          20       HibernateSession.currentSession().saveOrUpdate(this);
          21       return _id;
          22    }
          23    public void delete() throws HibernateException, SQLException {
          24       HibernateSession.currentSession().delete(this);
          25    }
          26    public void refresh() throws HibernateException, SQLException {
          27       HibernateSession.currentSession().load(this, _id);
          28    }
          29    public void lock() throws HibernateException, SQLException {
          30       HibernateSession.currentSession().lock(this, LockMode.UPGRADE);
          31    }
          32 
          33    public boolean onSave(Session s) throws CallbackException {
          34       return NO_VETO;
          35    }
          36    public boolean onDelete(Session s) throws CallbackException {
          37       return NO_VETO;
          38    }
          39    public boolean onUpdate(Session s) throws CallbackException {
          40       return NO_VETO;
          41    }
          42    public void onLoad(Session s, Serializable id) {
          43       _id = id;
          44    }
          45 
          46    public void validate() throws ValidationFailure {
          47    }
          48 }

          原文:http://hibernate.bluemars.net/46.html?cmd=prntdoc

          下面是hibernatesession的源碼,應(yīng)該考濾一下如何與spring事務(wù)結(jié)合的問(wèn)題(還是加一個(gè)doInTransaction 的callback?)
           1 import java.util.Collection;
           2 
           3 import net.sf.hibernate.HibernateException;
           4 import net.sf.hibernate.Session;
           5 import net.sf.hibernate.Transaction;
           6 
           7 import org.apache.commons.logging.Log;
           8 import org.apache.commons.logging.LogFactory;
           9 
          10 /**
          11  * @author Ralph Schaer
          12  * @version $Revision: 1.6 $ $Date: 2004/05/22 12:24:32 $
          13  */
          14 public class HibernateSession {
          15 
          16   private static final Log LOG = LogFactory.getLog(HibernateSession.class);
          17   public static final ThreadLocal SESSION = new ThreadLocal();
          18 
          19   public static Session currentSession() throws HibernateException {
          20 
          21     Session s = (Session) SESSION.get();
          22     if (s == null) {
          23       s = HibernateFactoryManager.getSessionFactory().openSession();
          24       SESSION.set(s);
          25     }
          26     return s;
          27   }
          28 
          29   public static Session getSession() throws HibernateException {
          30     return HibernateFactoryManager.getSessionFactory().openSession();
          31   }
          32 
          33   public static void closeSession() {
          34     try {
          35       Session s = (Session) SESSION.get();
          36       SESSION.set(null);
          37       if (s != null) {
          38         s.close();
          39       }
          40 
          41     } catch (HibernateException e) {
          42       LOG.error("HibernateSession:  closeSession", e);
          43     }
          44 
          45   }
          46 
          47   public static void rollback(Transaction tx) {
          48     if (tx != null) {
          49       try {
          50         tx.rollback();
          51         closeSession();
          52       } catch (HibernateException he) {
          53         LOG.error("HibernateSession: rollback", he);
          54       }
          55     }
          56   }
          57 
          58   //Utility methods
          59   public static int collectionSize(Collection coll) throws HibernateException {
          60     return ((Integer) currentSession().createFilter(coll, "select count(*)").iterate().next()).intValue();
          61   }
          62 
          63 }


          還有一種考慮泛型的方式可以參考:
          http://privacyneed.info/index.php?hl=f5&q=uggc%3A%2F%2Fcrgreonpxyhaq.oybtfcbg.pbz%2F2007%2F07%2Fvzcyrzragvat-npgvirerpbeq-va-wnin.ugzy
          posted on 2008-03-28 11:30 liunix 閱讀(483) 評(píng)論(0)  編輯  收藏

          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 玉山县| 尼勒克县| 平谷区| 西乌珠穆沁旗| 常德市| 安义县| 新晃| 嘉荫县| 册亨县| 博罗县| 黔南| 逊克县| 阜新市| 瑞昌市| 怀宁县| 民权县| 安化县| 长葛市| 西峡县| 贵港市| 双流县| 醴陵市| 全南县| 油尖旺区| 潞西市| 睢宁县| 沧州市| 福贡县| 绥滨县| 华宁县| 舟山市| 资阳市| 怀来县| 贵州省| 建平县| 全州县| 华坪县| 禄丰县| 贺兰县| 社会| 宜兰市|