hibernate - a different object with the same identifier value was already associated with the session error on save -
possible duplicate:
spring + hibernate : different object same identifier value associated session
i've been having problems hibernate annotations. have bidirectional relationship between 2 classes. here's mapping(thanks axtavt):
@entity public class receipt implements serializable { @id @generatedvalue(strategy=generationtype.auto) private long id; @onetomany(cascade = cascadetype.all, mappedby = "receipt") private list<collection> collections; ... } @entity public class collection implements serializable { @id @generatedvalue(strategy=generationtype.auto) private long id; @manytoone @joincolumn(name="receiptid") private receipt receipt; ... }
but when try save receipt list of collections using:
receipt r = new receipt(); list<collection> cols = new arraylist<collection>(); cols.add(new collection()); r.setcollections(cols); gethibernatetemplate().save(r);
it generates error:
org.springframework.orm.hibernate3.hibernatesystemexception: different object same identifier value associated session: [com.coa.acctreports.pojo.collection#0]; nested exception org.hibernate.nonuniqueobjectexception: different object same identifier value associated session: [com.coa.acctreports.pojo.collection#0] @ org.springframework.orm.hibernate3.sessionfactoryutils.converthibernateaccessexception(sessionfactoryutils.java:679) @ org.springframework.orm.hibernate3.hibernateaccessor.converthibernateaccessexception(hibernateaccessor.java:412) @ org.springframework.orm.hibernate3.hibernatetemplate.doexecute(hibernatetemplate.java:411) @ org.springframework.orm.hibernate3.hibernatetemplate.executewithnativesession(hibernatetemplate.java:374) @ org.springframework.orm.hibernate3.hibernatetemplate.save(hibernatetemplate.java:683) @ com.coa.acctreports.daoimp.accountingreportsimpl.save(accountingreportsimpl.java:35) @ sun.reflect.nativemethodaccessorimpl.invoke0(native method) @ sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:39) @ sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:25)
but when change
session.merge(receipt)
it has no errors when check database receiptid fk on colllections table set null... appreciated. ^_^...
the mappedby
annotation on receipt
means collection
owning side of relationship, not intended since have cascade on receipt
.
you should remove mappedby
on receipt
, follow example hibernate documentation:
@entity public class receipt implements serializable { @id @generatedvalue(strategy=generationtype.auto) private long id; @onetomany(cascade = cascadetype.all) @joincolumn(name="receiptid") private list<collection> collections; ... } @entity public class collection implements serializable { @id @generatedvalue(strategy=generationtype.auto) private long id; @manytoone @joincolumn(name="receiptid",insertable=false,updateable=false) private receipt receipt; ... }
using same code have above perform save should work.
there more information on here: http://docs.jboss.org/hibernate/annotations/3.5/reference/en/html_single/
Comments
Post a Comment