2010-06-01 39 views
0

可能重复:
Hibernate: different object with the same identifier value was already associated with the sessionHibernate对象的平等检查

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

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

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

注意:我的问题不是关于Hibernate持久化对象中的issues of implementing equal() or hashCode()方法。

谢谢。

回答

1

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

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