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