I ran into a problem in our shop, as relates to this. I'm sure somebody will correct me on this.
Hibernate suggests using business keys for equality, so that it can properly identify it's "identity", that is, roughly what row in the database the object represents.
A person with ID, Name, Address, Phone, nickname, whatever might be identified with a business key of just Name, and maybe Address if that isn't likely to change, if i understand the notion of a business key correctly. Fair enough.
In this way, 2 instances of this object with only a different nickname will appear to be the same object, because it's not included in the business key and equals() returns true.
I get all that, and i get why it's needed.
Problem i ran into is in our GUI/Swing portion of our application. They also use equals(). They use it to indicate a change to the entity took place, so a new view can be rendered. In their case, they need to know the above 2 instances are NOT equal, so that the correct listeners are fired and the new object replaces the old, and the nickname will be displayed.
So, we have these 2 objects. Pesistence needs these 2 objects to be equal, the GUI needs these two object to be !equal.
Caused a real rumble in our group. Both seem very reasonable.
The point i'm making is simply the choice of overriding and using .equals(). Clearly there can be many other application contexts that rely on .equals() to work in a slightly different way.
I don't have any answers to this, but since i haven't heard anybody complain about it, i'm wondering if i'm missing something real obvious or am overanalyzing it or something. Any thoughts?
FWIW, i suggested it was a problem they needed to solve - i guess they figured something out.
|