1

For our JPA entities we are overriding the equals and hashCode methods. I think the definitions are generated by Netbeans (I don't usually include equals or hashCode in my entities personally, but our other developers do)

I haven't ever encountered a problem in our application by not including these methods so I am wondering if I should just remove them.

If this is a really bad idea, then how thoroughly do I need to test the equals() method? Am I going to need to write tests for each of the conditions in the equals() contract ? For example a test for each of the following:

Reflexive: for any reference value x, x.equals(x) should return true.
Symmetric: for any reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
Transitive: for any reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
Consistent: for any reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the object is modified.
For any non-null reference value x, x.equals(null) should return false. 
  • depends if the entities are stored in Collections (e.g 1-N, M-N relations), are they? It is generally a good idea to have them for those situations IMHO – Neil Stockton Jan 14 '16 at 07:20
  • Yes they are usually stored in collections. So then on to the 2nd part of the question... am I going to test each of the clauses above in the equals contract()? – Duran Wesley Harris Jan 14 '16 at 07:21
  • 1
    Have a look at https://stackoverflow.com/questions/5031614/the-jpa-hashcode-equals-dilemma – Neil Stockton Jan 14 '16 at 07:22
  • That's an interesting post... didn't realize there were so many other things to consider – Duran Wesley Harris Jan 14 '16 at 07:27
  • So it seems the equals methods that we currently have need to be changed as we aren't using the business key to check equality. Thanks! – Duran Wesley Harris Jan 14 '16 at 07:36
  • In the table in this post https://stackoverflow.com/questions/5031614/the-jpa-hashcode-equals-dilemma that you mentioned, where they are comparing the effects of different equals implementations..One of the factors they consider is equality of the object in a different session. In what situation would I want consider the same entity from 2 different sessions as different. I'm just trying to visualize this... would someone log int to the same application from 2 different browser instances? – Duran Wesley Harris Jan 14 '16 at 07:58
  • IDE generated equals and hash code methods are just lazy. For entities write a proper equals() and hashCode() based on some business key (if that is the JPA ID then take the points in the the link above into consideration). Overriding equals() an hashcode() using all fields of a class should only be done for what Fowler refers to as Value Objects http://martinfowler.com/bliki/ValueObject.html – Alan Hay Jan 14 '16 at 08:58

0 Answers0