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

About giannigar

Senior Software Engineer - Groupon - Dublin, Ireland

Posted on February 21, 2014, in JPA, Programming and tagged , , , , . Bookmark the permalink. Leave a comment.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

The Bright Side of Java & Linux

Yet another Java blog

mysoftwarequality

Bringing empathy into knowledge workers life

Jess & the City

Cynical ramblings of a Latin girl living in Dublin

%d bloggers like this: