2013-02-21 87 views
1

我想知道爲什麼會發生此異常,儘管它通過Hibernate的get/query方法確保可以檢索Hibernate中已有的對象會議的情況下這些對象被重複要求:Hibernate:org.hibernate.NonUniqueObjectException:具有相同標識符值的不同對象已與會話相關聯

org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: [x.y.z.MyObject#130062] 
    at org.hibernate.engine.StatefulPersistenceContext.checkUniqueness(StatefulPersistenceContext.java:638) 
    at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performUpdate(DefaultSaveOrUpdateEventListener.java:305) 
    at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsDetached(DefaultSaveOrUpdateEventListener.java:246) 
    at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:112) 
    at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:93) 
    at org.hibernate.impl.SessionImpl.fireSaveOrUpdate(SessionImpl.java:685) 
    at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:677) 
    at org.hibernate.engine.CascadingAction$5.cascade(CascadingAction.java:252) 
    at org.hibernate.engine.Cascade.cascadeToOne(Cascade.java:392) 
    at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:335) 
    at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:204) 
    at org.hibernate.engine.Cascade.cascadeCollectionElements(Cascade.java:425) 
    at org.hibernate.engine.Cascade.cascadeCollection(Cascade.java:362) 
    at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:338) 
    at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:204) 
    at org.hibernate.engine.Cascade.cascade(Cascade.java:161) 
    at org.hibernate.engine.Cascade.cascade(Cascade.java:127) 
    at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.cascadeOnUpdate(DefaultSaveOrUpdateEventListener.java:376) 
    at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performUpdate(DefaultSaveOrUpdateEventListener.java:350) 
    at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsDetached(DefaultSaveOrUpdateEventListener.java:246) 
    at org.hibernate.event.def.DefaultUpdateEventListener.performSaveOrUpdate(DefaultUpdateEventListener.java:57) 
    at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:93) 
    at org.hibernate.impl.SessionImpl.fireUpdate(SessionImpl.java:742) 
    at org.hibernate.impl.SessionImpl.update(SessionImpl.java:730) 
    at org.hibernate.impl.SessionImpl.update(SessionImpl.java:722) 

回答

2

一些調試,我發現,我沒有阻止級聯一個自參照模型內更新的傳播後:

TYPE1 - > 2型 - > type3 - > collection(type1)

所以所有需要做是爲了阻止與cascade="none"級聯:

<set name="type1collection" ... cascade="none"> 
... 
</set> 

這意味着,用同樣的標識值不同的對象已經與會議相關被藏在裏面的自我指兒童。這些孩子的額外更新是不必要的,所以沒有級聯是正確的。

相關問題