Hibernate:我应该包含“版本”吗?字段到 hashcode() 和 equals() 方法
我知道,当重写持久实体的 hashcode()
和 equals()
时,我不应该包含 ID,而只包含唯一标识该对象的有意义的属性。 但是 Hibernate 用于乐观并发控制的 version
字段又如何呢?我应该像 ID 一样跳过它吗?如果我们说 new User(name='John', version=1).equals(new User(name='John',version=2))
,它不会让 Hibernate OCC 感到困惑吗? ?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
建议您使用业务键相等来实现 equals() 和 hashCode()。业务键相等意味着 equals() 方法仅比较构成业务键的属性。它是在现实世界中识别我们的实例的键(自然候选键),
因此您不应该在
equals()
中包含 version 属性hashcode( )
请参阅:http://docs.jboss.org/hibernate/core/3.3/reference/en/html/persistent-classes.html#persistent-classes-equalshashcode
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