Hallo.
Ich verwende Hibernate 3.1.3 und versuche, ein Objekt zu kopieren, welches wiederum Objekte und insbesondere auch Collections enthält.
Das Kopieren eines Objetes, das ichaus der DB hole klappt prima und Hibernate speichert auch den Klon ordentlich in die DB zurück (bzw. speichert diesen vielmehr erstmals).
Wenn ich aber versuche, auch die Sets des Originals zu kopieren und zu speichern, dann erhalte ich immer eine StaleStateExcpetion.
Bei den Collections handelt es sich um die Beziehungstabellen zwischen dem kopierten Objekt und einem anderen Objekt. Die kopierte Klasse heisst NiProject und in einer Collection sind NiProjectProjectScope-Instanzen.
DB-seitig habe ich also eine Tabelle Table_NiProject und eine Tabelle Table_NiProjectScope. dazwischen liegt die Tabelle TableProjectProjectScope.
D.h., ich möchte beim (bzw. nach dem) Kopieren in Table_ProjectProjectScope den projectScope-Eintrag unverändert lassen und lediglich meine NiProject-ID eintragen. allerdings muss es ja ein neuer Eintrag in der Beziehungstabelle sein, denn der alte soll ja nach wie vor bestehen bleiben.
Hier die Exception (obwohl ich beim Set angegeben habe cascade="all-delete-orphan"):
Code:
2006-12-18 16:05:36,225 ERROR [org.hibernate.event.def.AbstractFlushingEventListener] - <Could not synchronize database state with session>
org.hibernate.StaleStateException: Unexpected row count: 0 expected: 1
at org.hibernate.jdbc.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:27)
at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:2204)
at org.hibernate.persister.entity.AbstractEntityPersister.updateOrInsert(AbstractEntityPersister.java:2118)
at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:2374)
at org.hibernate.action.EntityUpdateAction.execute(EntityUpdateAction.java:91)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:248)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:232)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:140)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:297)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:985)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:333)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
at org.springframework.orm.hibernate3.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:584)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:500)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:473)
at org.springframework.transaction.interceptor.TransactionAspectSupport.doCommitTransactionAfterReturning(TransactionAspectSupport.java:267)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:106)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:170)
at org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:623)
at com.siemens.niapp.manager.NiManager$$EnhancerByCGLIB$$9ad54d5f.copyProject(<generated>)
at com.siemens.niapp.controller.createproject.CreateProjectCopyController.onSubmit(CreateProjectCopyController.java:54)
at org.springframework.web.servlet.mvc.SimpleFormController.processFormSubmission(SimpleFormController.java:258)
at org.springframework.web.servlet.mvc.AbstractFormController.handleRequestInternal(AbstractFormController.java:249)
at org.springframework.web.servlet.mvc.AbstractController.handleRequest(AbstractController.java:153)
at org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:44)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:723)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:663)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:394)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:358)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at com.opensymphony.module.sitemesh.filter.PageFilter.parsePage(PageFilter.java:118)
at com.opensymphony.module.sitemesh.filter.PageFilter.doFilter(PageFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:174)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:77)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:869)
at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:664)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
at java.lang.Thread.run(Thread.java:595)
2006-12-18 16:05:36,257 ERROR [org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/NIApp].[NIApp]] - <Servlet.service() for servlet NIApp threw exception>
org.springframework.orm.hibernate3.HibernateOptimisticLockingFailureException: Unexpected row count: 0 expected: 1; nested exception is org.hibernate.StaleStateException: Unexpected row count: 0 expected: 1
org.hibernate.StaleStateException: Unexpected row count: 0 expected: 1
at org.hibernate.jdbc.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:27)
at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:2204)
at org.hibernate.persister.entity.AbstractEntityPersister.updateOrInsert(AbstractEntityPersister.java:2118)
at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:2374)
at org.hibernate.action.EntityUpdateAction.execute(EntityUpdateAction.java:91)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:248)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:232)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:140)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:297)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:985)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:333)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
at org.springframework.orm.hibernate3.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:584)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:500)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:473)
at org.springframework.transaction.interceptor.TransactionAspectSupport.doCommitTransactionAfterReturning(TransactionAspectSupport.java:267)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:106)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:170)
at org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:623)
at com.siemens.niapp.manager.NiManager$$EnhancerByCGLIB$$9ad54d5f.copyProject(<generated>)
at com.siemens.niapp.controller.createproject.CreateProjectCopyController.onSubmit(CreateProjectCopyController.java:54)
at org.springframework.web.servlet.mvc.SimpleFormController.processFormSubmission(SimpleFormController.java:258)
at org.springframework.web.servlet.mvc.AbstractFormController.handleRequestInternal(AbstractFormController.java:249)
at org.springframework.web.servlet.mvc.AbstractController.handleRequest(AbstractController.java:153)
at org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:44)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:723)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:663)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:394)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:358)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at com.opensymphony.module.sitemesh.filter.PageFilter.parsePage(PageFilter.java:118)
at com.opensymphony.module.sitemesh.filter.PageFilter.doFilter(PageFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:174)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:77)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:869)
at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:664)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
at java.lang.Thread.run(Thread.java:595)
Hier die NiProject.hbm.xml-Datei (inkl. Copy-Constructor, einige mappings entfernt):Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping>
<class
name="com.siemens.niapp.model.NiProject"
table="Table_Ni_Project"
>
<meta attribute="extra-import">
java.util.Iterator
</meta>
<meta attribute="class-code">
/**
* Clones/copies an existing <code>NiProject</code> instance.
* If <code>deepCopy</code> is omitted (is null) or <code>false</code>
* only the primitive values (including wrapper classes) will be cloned.
*
* @param niProject The NiProject instance to be cloned.
* @param deepCopy Specifies, whether only a <em>flat</em>
* copy (copying just simple types) or a <em>deep</em>
* copy (copying objects as well) shall be performed.
*/
public NiProject(NiProject niProject, Boolean deepCopy)
{
if ( niProject == null )
{
return;
}
/*Integer*/this.projectId = niProject.projectId;
/*String*/this.projectTitle = niProject.projectTitle;
/*String*/this.amsSurname = niProject.amsSurname;
/*String*/this.amsGivenname = niProject.amsGivenname;
/*String*/this.amcSurname = niProject.amcSurname;
/*String*/this.amcGivenname = niProject.amcGivenname;
/*String*/this.pmsSurname = niProject.pmsSurname;
/*String*/this.pmsGivenname = niProject.pmsGivenname;
/*String*/this.pmcSurname = niProject.pmcSurname;
/*String*/this.pmcGivenname = niProject.pmcGivenname;
/*String*/this.cmSurname = niProject.cmSurname;
/*String*/this.cmGivenname = niProject.cmGivenname;
/*String*/this.pmSurname = niProject.pmSurname;
/*String*/this.pmGivenname = niProject.pmGivenname;
/*String*/this.remark = niProject.remark;
/*boolean*/this.kpiRelevant = niProject.kpiRelevant;
/*boolean*/this.ptaAvailable = niProject.ptaAvailable;
/*boolean*/this.markedAsDeleted = niProject.markedAsDeleted;
/*int*/this.numberOfSites = niProject.numberOfSites;
/*int*/this.duration = niProject.duration;
/*double*/this.volume = niProject.volume;
/*double*/this.volumeTotal = niProject.volumeTotal;
/*double*/this.probability = niProject.probability;
/*double*/this.budgetPta = niProject.budgetPta;
/*double*/this.budgetActual = niProject.budgetActual;
/*Date*/this.tenderStart = niProject.tenderStart != null ? new Date(niProject.tenderStart.getTime()) : null;
/*Date*/this.projectStart = niProject.projectStart != null ? new Date(niProject.projectStart.getTime()) : null;
/*Date*/this.pacPta = niProject.pacPta != null ? new Date(niProject.pacPta.getTime()) : null;
/*Date*/this.pacActual = niProject.pacActual != null ? new Date(niProject.pacActual.getTime()) : null;
/*Date*/this.fac = niProject.fac != null ? new Date(niProject.fac.getTime()) : null;
/*Date*/this.projectCreated = niProject.projectCreated != null ? new Date(niProject.projectCreated.getTime()) : null;
/*Date*/this.lastUpdate = niProject.lastUpdate != null ? new Date(niProject.lastUpdate.getTime()) : null;
if ( deepCopy == null || deepCopy == Boolean.TRUE )
{
/*Employee*/this.employeeByPmcId = niProject.employeeByPmcId;
/*Employee*/this.employeeByCountryManager = niProject.employeeByCountryManager;
/*Employee*/this.employeeByProjectManager = niProject.employeeByProjectManager;
/*Employee*/this.employeeByAmcId = niProject.employeeByAmcId;
/*Employee*/this.employeeByCreatedBy = niProject.employeeByCreatedBy;
/*Employee*/this.employeeByAmsId = niProject.employeeByAmsId;
/*Employee*/this.employeeByUpdateBy = niProject.employeeByUpdateBy;
/*Employee*/this.employeeByPmsId = niProject.employeeByPmsId;
/*Set*/
/*
this.niProjectProjectScopes = niProject.niProjectProjectScopes;
this.niTechnologyContainers = niProject.niTechnologyContainers;
this.niStaffings = niProject.niStaffings;
*/
//this.niProjectProjectScopes.addAll(niProject.niProjectProjectScopes);
Iterator iter = niProject.niProjectProjectScopes.iterator();
while (iter.hasNext() )
{
NiProjectProjectScope ppScope = (NiProjectProjectScope)iter.next();
NiProjectScope niProjectScope = ppScope.getNiProjectScope();
NiProjectProjectScope newPpScope = new NiProjectProjectScope();
newPpScope.setNiProject(this);
newPpScope.setScopeId(new Integer(0));
newPpScope.setNiProjectScope(niProjectScope);
this.niProjectProjectScopes.add(newPpScope);
}
//this.niTechnologyContainers.addAll(niProject.niTechnologyContainers);
iter = niProject.niTechnologyContainers.iterator();
while (iter.hasNext() )
{
NiTechnologyContainer tech = (NiTechnologyContainer)iter.next();
tech.setNiProject(this);
this.niProjectProjectScopes.add(tech);
}
//this.niStaffings.addAll(niProject.niStaffings);
iter = niProject.niStaffings.iterator();
while (iter.hasNext() )
{
NiStaffing staffing = (NiStaffing)iter.next();
staffing.setNiProject(this);
this.niProjectProjectScopes.add(staffing);
}
/*Country*/
this.country = niProject.country;
//this.country.setCountryId(country != null ? country.getCountryId() : new Integer(0));
/*NiClassification*/
this.niClassification = niProject.niClassification;
//this.niClassification.setClassificationId(niClassification != null ? niClassification.getClassificationId() : new Integer(0));
/*NiCategory*/
this.niCategory = niProject.niCategory;
//this.niCategory.setCategoryId(niCategory != null ? niCategory.getCategoryId() : new Integer(0));
/*NiLegalStatus*/
this.niLegalStatus = niProject.niLegalStatus;
//this.niLegalStatus.setLegalStatusId(niLegalStatus != null ? niLegalStatus.getLegalStatusId() : new Integer(0));
/*NiRegion*/
this.niRegion = niProject.niRegion;
//this.niRegion.setRegionId(niRegion != null ? niRegion.getRegionId() : new Integer(0));
/*NiProjectType*/
this.niProjectType = niProject.niProjectType;
//this.niProjectType.setProjectTypeId(niProjectType != null ? niProjectType.getProjectTypeId() : new Integer(0));
/*NiCustomer*/
this.customer = niProject.customer;
//this.customer = new NiCustomer(niProject.customer);
//this.customer.setCustomerId(niProject.customer != null ? niProject.customer.getCustomerId() : new Integer(0));
/*NiGlobalAccount*/
this.globalAccount = niProject.globalAccount;
//this.globalAccount.setGlobalAccountId(niProject.globalAccount != null ? niProject.globalAccount.getGlobalAccountId() : new Integer(0));
}//end if ( deepCopy )
}//end copy-constructor
//-------------------------------------------------------------------------
</meta>
<id
name="projectId"
type="java.lang.Integer"
column="Project_ID"
unsaved-value="0"
>
<generator class="identity" />
</id>
<!-- bi-directional one-to-many association to NiProjectProjectScope -->
<set
name="niProjectProjectScopes"
lazy="true"
inverse="true"
cascade="all-delete-orphan"
>
<key>
<column name="Project_ID" />
</key>
<one-to-many
class="com.siemens.niapp.model.NiProjectProjectScope"
/>
</set>
<!-- bi-directional one-to-many association to NiTechnologyContainer -->
<set
name="niTechnologyContainers"
lazy="true"
inverse="true"
cascade="none"
>
<key>
<column name="Project_ID" />
</key>
<one-to-many
class="com.siemens.niapp.model.NiTechnologyContainer"
/>
</set>
<!-- bi-directional one-to-many association to NiStaffing -->
<set
name="niStaffings"
lazy="true"
inverse="true"
cascade="none"
>
<key>
<column name="Project_ID" />
</key>
<one-to-many
class="com.siemens.niapp.model.NiStaffing"
/>
</set>
</class>
</hibernate-mapping>
Hier die NiProjectProjectScope.hbm.xml-Datei:Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping>
<!--
Created by the Middlegen Hibernate plugin 2.1
http://boss.bekk.no/boss/middlegen/
http://www.hibernate.org/
-->
<class
name="com.siemens.niapp.model.NiProjectProjectScope"
table="Table_Ni_Project_ProjectScope"
>
<id
name="scopeId"
type="java.lang.Integer"
column="Scope_ID"
>
<generator class="identity" />
</id>
<!-- Associations -->
<!-- bi-directional many-to-one association to NiProject -->
<many-to-one
name="niProject"
class="com.siemens.niapp.model.NiProject"
not-null="true"
>
<column name="Project_ID" />
</many-to-one>
<!-- bi-directional many-to-one association to NiProjectScope -->
<many-to-one
name="niProjectScope"
class="com.siemens.niapp.model.NiProjectScope"
not-null="true"
>
<column name="Project_Scope_ID" />
</many-to-one>
</class>
</hibernate-mapping>
Wie also kann ich ein Set kaskadiert speichern (neu anlegen)?