<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    Junky's IT Notebook

    統(tǒng)計(jì)

    留言簿(8)

    積分與排名

    WebSphere Studio

    閱讀排行榜

    評(píng)論排行榜

    Distributed Objects With Hibernate

    Distributed Objects With Hibernate

    Problem:

    You are building a three tier distributed system and you are passing persistent objects between tiers. When you send the objects back to the server, you want to save the only the changes.

    Solutions:


    1)Hibernate added the select-before-update option in 2.1. Setting this option on your class will cause Hibernate to load the data from the database and compare that to the data in your object to determine what has changed. It makes use of a version or timestamp field to enforce optimistic concurrency. This works well but introduces additional database access that could effect performance.

    2)Save the original data for each persistent object and implement a custom EntityPersister to return the original state to Hibernate instead of accessing the state in the database.

    Implementing Solution 2:

    Step 1: Save the original state for each persistent object

    The easiest way to implement this is to have each persistent object remember the original values of any properties that changed. If you make all your persistent objects proper JavaBeans, you can capture this in the base class when the object fires a propertyChanged event:

    public abstract class AbstractDomainObject implements Serializable {
    private PropertyChangeSupport support = new PropertyChangeSupport(this);
    private Map originalValues = new HashMap();
    public AbstractDomainObject() {
    super();
    }
    public Map getOriginalState() {
    return originalValues;
    }
    public void addPropertyChangeListener(PropertyChangeListener propertyChangeListener) {
    support.addPropertyChangeListener(propertyChangeListener);
    }
    protected void firePropertyChange(String property, Object oldValue, Object newValue) {
    if (!originalValues.containsKey(property)) {
    originalValues.put(property, oldValue);
    }
    support.firePropertyChange(property, oldValue, newValue);
    }
    }

    Step 2: Create your domain objects

    A domain object might look like follows. Because it fires propertyChange events, the base class will remember original values of properties that changed. Note that you will want to map the properties as access=field since the setter fires a propertyChange event when called.

    public class Customer extends AbstractDomainObject {
    private String name;
    public String getName() {
    return name;
    }
    public void setName(String name) {
    String oldValue = this.name;
    this.name = name;
    firePropertyChange("name", oldValue, name);
    }
    }

    Step 3: Create a custome entity persister

    Create a custom entity persister and override getCurrentPersistentState. Set the persister for each class to this custom persister.

    public Object[] getCurrentPersistentState(Serializable id, Object version, SessionImplementor session) throws HibernateException {
    AbstractDomainObject entity = (AbstractDomainObject) session.getEntity(new Key(id, this));
    Map originalValues = entity.getOriginalState();
    Type[] types = getPropertyTypes();
    String[] propertyNames = getPropertyNames();
    Object[] values = new Object[propertyNames.length];
    boolean[] includeProperty = getPropertyUpdateability();
    for (int i = 0; i < propertyNames.length; i++) {
    if (includeProperty[i]) {
    if (originalValues.containsKey(propertyNames[i])) {
    values[i] = types[i].disassemble(originalValues.get(propertyNames[i]), session);
    } else {
    values[i] = types[i].disassemble(getPropertyValue(entity, propertyNames[i]), session);
    }
    }
    }
    return values;
    }

    What about collections you ask? Well, Hibernate keeps track of changes to collections in the collection class wrappers for you.

    Please note this solution has worked great in my example programs, but I have not yet implemented it in a production program.

    posted on 2006-10-17 14:09 junky 閱讀(405) 評(píng)論(0)  編輯  收藏 所屬分類: hibernate

    主站蜘蛛池模板: 亚洲国产综合精品中文第一区| 亚洲精品成a人在线观看夫| 日本人的色道免费网站| 亚洲色大成网站www尤物| 国产精品亚洲玖玖玖在线观看| 日韩午夜理论免费TV影院| 亚洲av无码专区在线| 亚洲男人的天堂在线va拉文| 久久国产免费一区二区三区| 亚洲熟女综合一区二区三区| 亚洲日韩欧洲乱码AV夜夜摸| 久久久www成人免费毛片| 巨胸喷奶水视频www免费视频 | 亚洲大片免费观看| 亚洲国产欧美日韩精品一区二区三区 | 久久中文字幕免费视频| 亚洲乱人伦中文字幕无码| 亚洲精品国产精品乱码在线观看| 免费AA片少妇人AA片直播| 精品久久久久久国产免费了| 亚洲伊人久久大香线蕉影院| 日韩亚洲变态另类中文| 女人让男人免费桶爽30分钟| 久久午夜夜伦鲁鲁片无码免费| 色噜噜的亚洲男人的天堂| 91情国产l精品国产亚洲区 | 亚洲欧洲精品成人久久曰| 亚洲av永久无码精品秋霞电影影院 | 久久精品国产亚洲av瑜伽| 亚洲人成影院在线| 无码精品国产一区二区三区免费| 亚洲国产另类久久久精品| 九九精品免费视频| 91国内免费在线视频| 免费看一级高潮毛片| 亚洲最大的成人网| 亚洲制服丝袜一区二区三区| 亚洲AV无码一区二区三区DV| 亚洲午夜精品一级在线播放放| 国产精品免费看久久久久| 女人张开腿等男人桶免费视频|