spring - hibernate @onetomany relationship updates instead of insert during save -
please me figure out. i've tried many combinations nothing seems work. i'm trying implement hibernate mapping using annotations during saving of parent object , children noticed update statement being called instead of insert statement.
i have 2 classes have one-to-many relationship each other.
these class' mappings: receipt has one-to-many collections
@entity public class receipt implements serializable { @id @generatedvalue(strategy=generationtype.auto) private long id; @onetomany @joincolumn(name="receiptid") private list<collection> collections; //setters, getters } @entity public class collection implements serializable{ @id @generatedvalue(strategy=generationtype.auto) private long id; @manytoone @joincolumn(name="receiptid", insertable=false, updatable=false) private receipt receipt; //setters getters }
the problem during saving receipt like:
receipt r = new receipt(); list<collection> cols = new arraylist<collection>(); cols.add(new collection()); r.setcollections(cols); gethibernatetemplate().save(r);
it generates error:
hibernate: insert receipt (userid, datecreated, payor, receiptdate, receiptnumber, total) values (?, ?, ?, ?, ?, ?) hibernate: update collection set receiptid=? id=? nov 15, 2010 8:46:00 pm org.hibernate.jdbc.batchingbatcher doexecutebatch severe: exception executing batch: org.hibernate.stalestateexception: batch update returned unexpected row count update [0]; actual row count: 0; expected: 1 @ org.hibernate.jdbc.expectations$basicexpectation.checkbatched(expectations.java:85) @ org.hibernate.jdbc.expectations$basicexpectation.verifyoutcome(expectations.java:70) @ org.hibernate.jdbc.batchingbatcher.checkrowcounts(batchingbatcher.java:90) @ org.hibernate.jdbc.batchingbatcher.doexecutebatch(batchingbatcher.java:70) @ org.hibernate.jdbc.abstractbatcher.executebatch(abstractbatcher.java:268) @ org.hibernate.engine.actionqueue.executeactions(actionqueue.java:266) @ org.hibernate.engine.actionqueue.executeactions(actionqueue.java:171) @ org.hibernate.event.def.abstractflushingeventlistener.performexecutions(abstractflushingeventlistener.java:321) @ org.hibernate.event.def.defaultflusheventlistener.onflush(defaultflusheventlistener.java:50) @ org.hibernate.impl.sessionimpl.flush(sessionimpl.java:1027) @ org.springframework.orm.hibernate3.hibernatetemplate$28.doinhibernate(hibernatetemplate.java:883) @ org.springframework.orm.hibernate3.hibernatetemplate.doexecute(hibernatetemplate.java:406) @ org.springframework.orm.hibernate3.hibernatetemplate.executewithnativesession(hibernatetemplate.java:374) @ org.springframework.orm.hibernate3.hibernatetemplate.flush(hibernatetemplate.java:881) @ com.coa.acctreports.daoimp.accountingreportsimpl.update(accountingreportsimpl.java:52) @ sun.reflect.nativemethodaccessorimpl.invoke0(native method) @ sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:39) @ sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:25) @ java.lang.reflect.method.invoke(method.java:597) @ org.springframework.aop.support.aoputils.invokejoinpointusingreflection(aoputils.java:309) @ org.springframework.aop.framework.reflectivemethodinvocation.invokejoinpoint(reflectivemethodinvocation.java:183) @ org.springframework.aop.framework.reflectivemethodinvocation.proceed(reflectivemethodinvocation.java:150) @ org.springframework.transaction.interceptor.transactioninterceptor.invoke(transactioninterceptor.java:110) @ org.springframework.aop.framework.reflectivemethodinvocation.proceed(reflectivemethodinvocation.java:172) @ org.springframework.aop.framework.jdkdynamicaopproxy.invoke(jdkdynamicaopproxy.java:202)
in case of bidirectional many-to-one/one-to-many relationship, "many" side should owning side (and @joincolumn
specified @ owning side), whereas "one" side should have mappedby
poiniting owning side. in case need enable cascading of save operation:
@entity public class receipt implements serializable { @onetomany(cascade = cascadetype.all, mappedby = "receipt") private list<collection> collections; ... } @entity public class collection implements serializable { @manytoone @joincolumn(name="receiptid") private receipt receipt; ... }
see also:
Comments
Post a Comment