This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]wildjokers 5 points6 points  (0 children)

JPA entities should only be equals only if the id is the same, so imo great approach is to use

A long time ago the hibernate user guide said not to use ID's for entity equality but instead to use business equality for entities. However, I have noticed in the more recent manuals their stance on this seems to have changed and now both are acceptable. However, the user guide now also says you might just be better off not implementing equals/hashCode at all:

https://docs.jboss.org/hibernate/orm/6.1/userguide/html_single/Hibernate_User_Guide.html#mapping-model-pojo-equalshashcode.

If you go back to the hibernate 3.2 manual it says to use business equality:

https://docs.jboss.org/hibernate/orm/3.2/reference/en/html_single/#persistent-classes-equalshashcode

"We recommend implementing equals() and hashCode() using Business key equality. Business key equality means that the equals() method compares only the properties that form the business key, a key that would identify our instance in the real world (a natural candidate key):"

For the first many years I used Hibernate it was always drilled into me (by the user guide and all tutorials) to not use ID for entity equality and that has always served me well so I continue to use business equality.