Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Any harm in adding equals()/hashCode() methods to an ORM class?

 
James Adams
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry if this is a bonehead question -- can I add an equals() and hashCode() method to a class which is used as an ORM object? For example I have several classes which I use for objects which are persisted using Hibernate. Will I be safe adding equals() and hashCode() methods to these classes, i.e. will the Hibernate persistence mechanism still work the same after these methods are added?

Thanks in advance for any helpful replies.


--James
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As a matter of fact, you should definitely override those methods, just don't use the PK, er Id field in the comparison. In cases where you are using detached objects, it is very important.

Mark
 
Jaikiran Pai
Marshal
Pie
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This might help:

Equals and Hashcode - Hibernate
 
Tim Holloway
Saloon Keeper
Posts: 18362
56
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sort answer for the lazy: Not only can you, but in many cases, you must. Not only in Hibernate, but in other ORM technologies like JDO and EJB.

These methods are used by the ORM framework to determine if two objects both refer to the same item in the backing store (database), as well as assisting in maintaining a local cache. If they don't work properly, you can corrupt your data.
 
Tim Holloway
Saloon Keeper
Posts: 18362
56
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sort answer for the lazy: Not only can you, but in many cases, you must. Not only in Hibernate, but in other ORM technologies like JDO and EJB.

These methods are used by the ORM framework to determine if two objects both refer to the same item in the backing store (database), as well as assisting in maintaining a local cache. If they don't work properly, you can corrupt your data.
 
Tim Holloway
Saloon Keeper
Posts: 18362
56
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, I'm not "AOL"ing. The BBS software stuttered and effectively double-posted my reply. The JavaRanch server has been seeing some strain lately.

Sorry for the annoyance.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic