Hibernate ? ActiveRecord ? pretty code!
看到了段代码,贴出来聊聊
public class Persistent implements Lifecycle, Validatable, Serializable { protected Long _id; protected int _version; protected boolean _versionCheckRequired; public Long getIdentifier() { return _id; } public void setIdentifier(Long id) { _id = id; } public int getVersion() { return _version; } public void setVersion(int version) { _version = version; } public Long persist() throws HibernateException, SQLException { HibernateSession.currentSession().saveOrUpdate(this); return _id; } public void delete() throws HibernateException, SQLException { HibernateSession.currentSession().delete(this); } public void refresh() throws HibernateException, SQLException { HibernateSession.currentSession().load(this, _id); } public void lock() throws HibernateException, SQLException { HibernateSession.currentSession().lock(this, LockMode.UPGRADE); } public void checkVersion(int version) throws StaleObjectException { if (version != _version) { throw new StaleObjectException(); } _versionCheckRequired = false; } public boolean onSave(Session s) throws CallbackException { return NO_VETO; } public boolean onDelete(Session s) throws CallbackException { return NO_VETO; } public boolean onUpdate(Session s) throws CallbackException { return NO_VETO; } public void onLoad(Session s, Serializable id) throws CallbackException { _versionCheckRequired = true; onLoad(s, (Long) id); } protected void onLoad(Session s, Long id) throws CallbackException { } public void validate() throws ValidationFailure { if (_versionCheckReqired) { throw new ValidationFailure("version check is required"); } }}
怎么样!? 1 楼 moonranger 2008-01-10 看起来不错 2 楼 andyao 2008-01-10 多个操作需要事务时,事务怎么处理?
3 楼 jasongreen 2008-01-11 一直未能理解事务,这个词一点都不直观,啥叫事务啊?
是指rollback那一块的东西吗?
4 楼 andyao 2008-01-11 我爱罗:)
你可以google一下,事务,了解一下事务的定义。
5 楼 jasongreen 2008-01-12 google了,的确是rollback相关的概念。
这个不影响,因为 事务是在 service层定义的东西,service层控制 session的 beginTransaction和 Model中只管
HibernateSession.currentSession().doSomthing(this);
6 楼 我想我是海 2008-01-13 的确是与事务无关。不过这种做法也不是新鲜的,以前的帖子有讨论过用Java实现Rails那样的风格的CURD不难。只要你可以忍受在实体里有Hibernate的API,这个时候你的实体已经不POJO了。。需要POJO吗?不需要POJO吗?个人喜好吧。 7 楼 jasongreen 2008-01-13 是啊,不是pojo了
在SOA的系统架构中,POJO还是很必要的