-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: EntityManager remove object mit many-to-one relationship
PostPosted: Wed Jul 25, 2007 7:42 am 
Newbie

Joined: Fri Jan 23, 2004 4:02 am
Posts: 6
Location: Linz, Austria
Ich verwende hibernate-3.2.3.ga, hibernate-entitymanager-3.2.1.ga mit Spring

Beim Löschen eines Activity Objektes kommt es zur unten angeführten Exception UnexpectedRollbackException. Das Activity Objekt ist Owner der Beziehung zum Objekt Project. Der Fehler tritt auch auf wenn vor dem Löschen die Beziehung entfernt wird - setProject(null) aufgerufen wird.

Es sieht aus als ob Hibernate versucht eine Cascading Operation auszuführen (siehe Stacktrace) obwohl auf Seite der many-to-one Beziehung kein Cascading in orm.xml konfiguriert ist.
Wird das Cascading auf Seite der one-to-many Beziehung entfernt dann funktioniert das Löschen!

Nun stellt sich die Frage ist das ein Problem von JPA (wieder einmal ein Feature das nicht im Standard vorgesehen ist) oder ob meinerseits ein Fehler vorliegt?


Code:
public class Project {
   private Long id;
   private Customer customer;
   private String name;
   private Set<Activity> activities;
...
}


Code:
public class Activity {
   private Long id;
   private DateRange date;
   private String description;
   private Project project;
   private boolean billable = true;
   private ActivityType activityType;
...
}

Code:
   <entity class="at.martinahrer.freelancer.model.Project">
      <attributes>
         <id name="id">
            <generated-value strategy="IDENTITY" />
         </id>
         <basic name="name"></basic>
         <one-to-many name="activities" fetch="EAGER" mapped-by="project" >
            <cascade>
               <cascade-persist />
               <cascade-merge />
            </cascade>
         </one-to-many>
         <transient name="customer" />
         <transient name="durationTotal" />
      </attributes>
   </entity>

   <entity class="at.martinahrer.freelancer.model.Activity">
      <attributes>
         <id name="id">
            <generated-value strategy="IDENTITY" />
         </id>
         <basic name="description"></basic>
         <basic name="billable"></basic>
         <many-to-one name="project"/>
         <many-to-one name="activityType"></many-to-one>
         <embedded name="date"></embedded>
         <transient name="billableDuration" />
         <transient name="duration" />
      </attributes>
   </entity>


Die unten angeführte JUnit Test Methode ruft die Service Methode auf und übergibt ein Activity Objekt (der Service ist durch Spring transaktional!)

Code:
   public void testRemoveActivity() {
      activityService.delete(activityService.find().get(0));
      return;
      // TODO assert
   }


activityService.delete -> (spring managed transaction) führt die JPA remove operation durch:

Code:
   public void delete(T object) throws DataAccessException {
      // JPA requires a detached object to be associated with the persistence
      // context before being deleted!
      getJpaTemplate().remove(getJpaTemplate().merge(object));
   }


Das übergebene Objekt ist detached, daher wird vor dem Löschen die merge Operation ausgeführt.


Code:
main 12:12:35,234  INFO SchemaUpdate:execute:160 - schema update complete

Hibernate: select activity0_.id as id3_, activity0_.endTime as endTime3_, activity0_.startTime as startTime3_, activity0_.description as descript4_3_, activity0_.activityType_id as activity6_3_, …
Hibernate: select project0_.id as id2_2_, project0_.name as name2_2_, activities1_.project_id as project7_4_, activities1_.id as id4_, activities1_.id as id3_0_, activities1_.endTime as endTime3_0_, …
Hibernate: select project0_.id as id2_2_, project0_.name as name2_2_, activities1_.project_id as project7_4_, activities1_.id as id4_, activities1_.id as id3_0_, activities1_.endTime as endTime3_0_,…
Hibernate: select project0_.id as id2_2_, project0_.name as name2_2_, activities1_.project_id as project7_4_, activities1_.id as id4_, activities1_.id as id3_0_, activities1_.endTime as endTime3_0_,… 

Hibernate: select activity0_.id as id3_0_, activity0_.endTime as endTime3_0_, activity0_.startTime as startTime3_0_, activity0_.description as descript4_3_0_, activity0_.activityType_id as activity6_3_0_, …
Hibernate: select project0_.id as id2_1_, project0_.name as name2_1_, activities1_.project_id as project7_3_, activities1_.id as id3_, activities1_.id as id3_0_, activities1_.endTime as endTime3_0_, …

flush() oder commit() ->

Code:
org.springframework.transaction.UnexpectedRollbackException: JPA transaction unexpectedly rolled back (maybe marked rollback-only after a failed operation); nested exception is javax.persistence.RollbackException: Error while commiting the transaction
Caused by: javax.persistence.RollbackException: Error while commiting the transaction
   at org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:71)
   at org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:433)
   at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:662)
   at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:632)
   at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:314)
   at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:117)
   at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:166)
   at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
   at $Proxy20.delete(Unknown Source)
   at at.martinahrer.freelancer.service.test.ProjectServiceTest.testRemoveActivity(ProjectServiceTest.java:62)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
   at java.lang.reflect.Method.invoke(Method.java:585)
   at junit.framework.TestCase.runTest(TestCase.java:164)
   at junit.framework.TestCase.runBare(TestCase.java:130)
   at junit.framework.TestResult$1.protect(TestResult.java:106)
   at junit.framework.TestResult.runProtected(TestResult.java:124)
   at junit.framework.TestResult.run(TestResult.java:109)
   at junit.framework.TestCase.run(TestCase.java:120)
   at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:130)
   at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
Caused by: org.hibernate.ObjectDeletedException: deleted entity passed to persist: [at.martinahrer.freelancer.model.Activity#<null>]
   at org.hibernate.event.def.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:90)
   at org.hibernate.impl.SessionImpl.firePersistOnFlush(SessionImpl.java:644)
   at org.hibernate.impl.SessionImpl.persistOnFlush(SessionImpl.java:636)
   at org.hibernate.engine.CascadingAction$9.cascade(CascadingAction.java:323)
   at org.hibernate.engine.Cascade.cascadeToOne(Cascade.java:268)
   at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:216)
   at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:169)
   at org.hibernate.engine.Cascade.cascadeCollectionElements(Cascade.java:296)
   at org.hibernate.engine.Cascade.cascadeCollection(Cascade.java:242)
   at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:219)
   at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:169)
   at org.hibernate.engine.Cascade.cascade(Cascade.java:130)
   at org.hibernate.event.def.AbstractFlushingEventListener.cascadeOnFlush(AbstractFlushingEventListener.java:131)
   at org.hibernate.event.def.AbstractFlushingEventListener.prepareEntityFlushes(AbstractFlushingEventListener.java:122)
   at org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:65)
   at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:26)
   at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
   at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338)
   at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
   at org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:54)
   ... 25 more


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.