-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Alternative to "select-before-update" for dynamicUpdate?
PostPosted: Mon Sep 28, 2009 10:57 am 
Newbie

Joined: Fri Sep 11, 2009 10:15 am
Posts: 8
Hello,

i want to use dynamicUpdate ("update only dirty fields") for my detached objects without using "select-before-update".
I also already found an article inside the wiki about this topic: https://www.hibernate.org/161.html "Distributed Objects With Hibernate"

Unfortunately this article is not for Hibernate version 3 and doesn't mention from which class to inherit (e.g. AbstractEntityPersister?)
Is there any simpler solution or can anyone tell me how to use this solution with Hibernate v.3 and Annotations?

Any help would be appreciated.

Update:
Here is my solution:
Code:
public class AbstractDomainObject {
   protected PropertyChangeSupport support = new PropertyChangeSupport(this);

   protected 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);
   }

}

public class MyPersister extends SingleTableEntityPersister {

   public MyPersister(PersistentClass arg0, EntityRegionAccessStrategy arg1,
         SessionFactoryImplementor arg2, org.hibernate.engine.Mapping arg3) throws HibernateException {
      super(arg0, arg1, arg2, arg3);
   }

   @Override
   public Object[] getDatabaseSnapshot(Serializable id,
         SessionImplementor session) throws HibernateException {
      
      AbstractDomainObject entity = (AbstractDomainObject) session
            .getEntityUsingInterceptor(new EntityKey(id, this,
                  EntityMode.POJO));
      
      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, null);
            } else {
               values[i] = types[i].disassemble(getPropertyValue(entity,
                     propertyNames[i], EntityMode.POJO), session, null);
            }
         }
      }
      return values;
   }
}


You have to set "select-before-update=true" and "persister=MyPersister.class". Should work with Hibernate v.3

Best regards

peter


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.