Hibernate对象相等性检查 [英] Hibernate object equality checking

查看:77
本文介绍了Hibernate对象相等性检查的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能存在重复:


Possible Duplicate:
Hibernate: different object with the same identifier value was already associated with the session

$ b同一个标识符值已经被关联了


$ b

据我了解(纠正我,如果我错了)Hibernate使用对象引用来检查对象的相等性。当Hibernate发现有多个对象附加到同一个数据库记录时,它会抛出以下异常。

As far as I understand(correct me if I am wrong) Hibernate uses object reference to check the object equality. When Hibernate identifies that there are more than one objects attached to same DB record, it throws following exception.

"a different object with the same identifier value was already associated with the session"

我的问题是,Hibernate使用 equal()方法检查对象是否相等(默认的equals方法使用对象引用)?如果是这样,重写 equal()方法会改变Hibernate的行为吗?

My question is, does Hibernate use equal() method to check the object equality (The default equal method uses object reference)? If it is true, will overridden equal() method change the Hibernate behavior?

注意:我的问题不是关于执行 equal()问题在Hibernate持久化对象中使用$ c>或 hashCode() 方法。

Note: My question is not about the issues of implementing equal() or hashCode() methods in a Hibernate persisted object.

谢谢。

推荐答案

Hibernate使用实体)和配置的ID。过度简化,这将看起来像这样

Hibernate uses the entity (i.e. class) and configured id. Over-simplified, this would look somehow like this

o1.getClass().equals(o2.getClass()) && o1.getId().equals(o2.getId())

这篇关于Hibernate对象相等性检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆