Blog Archives

Equals and hashcode how to for Hibernate and JPA in general


This morning I was manually writing a Hibernate bean and, when I got to question myself whether or not implementing equals and hashcode, I stumbled upon quite a few dilemmas:

  • shall I create them at all? (well yeah coz you want to use them in collections and the comparison between two objects should rely upon their pk and determine if they’re the same. But does the provider even care about it? )
  • will the pk be enough to implement equals and hashcode?

I then checked other entities in the project and found out all of them were using all the attributes for determining equality and hash. WT*??

As usual google is your friend and TADAAAA!!! One of the best written articles I’ve read recently.

Equals and Hashcode, all you need to know for your java JPA provider!

https://community.jboss.org/wiki/EqualsAndHashCode

Kudos to the writer

The Bright Side of Java & Linux

Yet another Java blog

mysoftwarequality

Bringing empathy into knowledge workers life

%d bloggers like this: