开发者

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

开发者 https://www.devze.com 2023-04-03 19:34 出处:网络
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.

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

0

精彩评论

暂无评论...
验证码 换一张
取 消