Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate: Should I include the 'version' field to hashcode() and equals() methods

I know that when overriding hashcode() and equals() of my persistent entities I should not include ID and only include the meaningful properties that uniquely identify the object. But what about version field which is used for the optimistic concurrency control by Hibernate? Should I skip it as well, just like ID? What if let's say new User(name='John', version=1).equals(new User(name='John',version=2)), won't it confuse Hibernate OCC anyhow?

like image 759
Alex Vayda Avatar asked Sep 10 '11 17:09

Alex Vayda


1 Answers

It is recommended that you implement equals() and hashCode() using Business key equality. Business key equality means that the equals() method compares only the properties that form the business key. It is a key that would identify our instance in the real world (a natural candidate key)

So you should not include version property in the equals() hashcode()

Refer: http://docs.jboss.org/hibernate/core/3.3/reference/en/html/persistent-classes.html#persistent-classes-equalshashcode

like image 175
Yogesh Kulkarni Avatar answered Oct 13 '22 13:10

Yogesh Kulkarni