There have been some discussions here about JPA entities and which hashCode()
/equals()
implementation should be used for JPA entity classes. Most (if not all) of them depend on Hibernate, but I'd like to discuss them JPA-implementation-neutrally (I am using EclipseLink, by the way).
All possible implementations are having their own advantages and disadvantages regarding:
hashCode()
/equals()
contract conformity (immutability) for List
/Set
operationsAs far I can see, there are three options:
Object.equals()
and Object.hashCode()
hashCode()
/equals()
workhashCode()
/equals()
are brokenhashCode()
/equals()
are brokenMy questions are:
UPDATE 1:
By "hashCode()
/equals()
are broken", I mean that successive hashCode()
invocations may return differing values, which is (when correctly implemented) not broken in the sense of the Object
API documentation, but which causes problems when trying to retrieve a changed entity from a Map
, Set
or other hash-based Collection
. Consequently, JPA implementations (at least EclipseLink) will not work correctly in some cases.
UPDATE 2:
Thank you for your answers -- most of them have remarkable quality.
Unfortunately, I am still unsure which approach will be the best for a real-life application, or how to determine the best approach for my application. So, I'll keep the question open and hope for some more discussions and/or opinions.
Introduction. As previously explained, using the JPA entity business key for equals and hashCode is always best choice. However, not all entities feature a unique business key, so we need to use another database column that is also unique, as the primary key.
If you take a look at the JPA specification, you will be surprised to only find 2 explicit and 1 implicit mention of both methods: You need to implement the equals() and hashCode() methods for primary key classes if you map composite primary keys.
You only need to override equals() and hashcode() if the entity will be used in a Set (which is very common) AND the entity will be detached from, and subsequently re-attached to, hibernate sessions (which is an uncommon usage of hibernate).
In other words, Hibernate uses equals and hashCode for identity, not to see if an object has been modified. It uses attribute by attribute comparisons for that.
Read this very nice article on the subject: Don't Let Hibernate Steal Your Identity.
The conclusion of the article goes like this:
Object identity is deceptively hard to implement correctly when objects are persisted to a database. However, the problems stem entirely from allowing objects to exist without an id before they are saved. We can solve these problems by taking the responsibility of assigning object IDs away from object-relational mapping frameworks such as Hibernate. Instead, object IDs can be assigned as soon as the object is instantiated. This makes object identity simple and error-free, and reduces the amount of code needed in the domain model.
I always override equals/hashcode and implement it based on the business id. Seems the most reasonable solution for me. See the following link.
To sum all this stuff up, here is a listing of what will work or won't work with the different ways to handle equals/hashCode:
EDIT:
To explain why this works for me:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With