Win a copy of Microservices Testing (Live Project) this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

equals hashCode furthermore needed?

 
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Am I right, that in JPA2 I do not need to overwrite equals/hashCode?

And If yes, am I right, that only the id-field(s) of a class should be considered in the equals/hashCode ?
 
Ranch Hand
Posts: 553
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am not aware of the JPA spec ever requiring you define an equals/hashCode in your Entity objects.

JPA does require you define equals/hashCode in IdClass or EmbeddedId classes. I don't think this requirement has changed in JPA2.

A JPA provider may have additional, or less requirements. (in EclipseLink you do not need to override equals/hashCode in your Entity, or normally even in your IdClass).

If you define an equals/hashCode in your Entity, sticking to Id fields would probably be a good idea, however be careful of null Ids for new objects, you probably don't want all new objects to be equal.
If null maybe revert to identity, or just use identity (Object.equals) in general as JPA preserves object identity within a persistence context.

If you use Sets or Maps ensure your objects hashCode will never change, otherwise this could corrupt the Set/Map's hashing.
 
nimo frey
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you! That helped me!
 
Good heavens! What have you done! Here, try to fix it with this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic