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 w开发者_运维问答hich 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?
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
精彩评论