随笔-69  评论-0  文章-5  trackbacks-0
去年rails正火的时候,仿active record用hibernate实现了一个BasePo,当时用的是一个
静态sessionfactory注入的方式,感觉很不好,当明也没想到好的方案,后来发现有人通过
threadlocal实现了下面这个,可以看看:
 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的源码,应该考滤一下如何与spring事务结合的问题(还是加一个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 阅读(458) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航: