gavin wrote:
... we certainly do not do something crap like only update non-null fields. And we never will.
Hi guys, long time no see. :)
Gavin, one of my colleagues is having to do updates now, and he queried me about how he would update any field, in any object. I told him that he'd probably have to session.load the object, search through the incomming object (XML) and see what is NOT NULL and then set (update) each field, as per your post.
But then I started thinking. Wouldn't it be possible, and I am sure that lots of people would really like it, that a generic method be created (within Hibernate), perhaps like this:
Code:
public Object updateObject (Object inOjbect, Object databaseObject)
Both objects would have to be of the same type, which is mapped via Hibernate. Hibernate would then, using the mapping file, traverse all fields in each object and examine them doing the basic:
Code:
// For each field:
If ((inObject.getfieldX() != null) && (!inObject.getFieldX().toString().equals(databaseObject.getFieldX().toString())) { // Obviously some form of Type casting needs to happen here... ?
databaseObject.setFieldX(inObject.getFieldX());
}
The alternative to this, is basically to traverse every single field manually and check which is null and different...for every type of object you use...and this is a LOT of PT.
As I said, it'd be REALLY nice if Hibernate could do this. :) And yes, I know you said you never will...but...well, that was a while back.
-G