One question:
Your UI gets the fully initialized entity right? For example:
id=1
firstName="John"
lastName="Smith"
Then, the someone changed the first name. Hence the firstName attribute is changed. All the other attribute, which aren't changed, are set to null. So the resulting entity is:
id=1
firstName="Tom"
lastName=null;
If the UI didn't alter the lastName, you wouldn't have any problems. I think this would be the best solution. I don't see why the UI should set the not changed attributes to null. What is the sense of it?
However, If you can't change the behaviour of your UI and you don't want to compare the object which you get from the ui with the record in the database, I suggest that you take the sollution posted above by batman. If you implement his sollution which aspects (with aspectj for example) you mustn't implement the bevaviour in each entity and for each property:
Code:
public aspect DontSetNullPropertiesAspect {
//all setter Methodes of all my entities
pointcut allSetter(Object attribute):
execution(* my.entity.package..*.set*(*)) && args(attribute);
Object around(Object attribute) : allSetter(attribute) {
if (attribute != null) {
//set the attribute
proceed(attribute);
}
return null;
}}
Code:
public class Person{
String firstName;
String name;
public void setFirstName(String fName)
{
//the null check is done by the aspect
this.firstName = fName;
}
...
// and so on
}
If you implement the behaviour with an aspect, your entity code won't be polluted, you'll have fewer code, it will apply for all your entities and it will be much more flexible. If you don't won't this behaviour in future (for example because you changed the behaviour of the UI), you will only have to delete the aspect and mustn't alter your entities.
One good bool about aspects: AspectJ in Action.
Hoeft