-->
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.  [ 3 posts ] 
Author Message
 Post subject: Removing entities with relationships
PostPosted: Mon Sep 18, 2006 6:59 am 
Senior
Senior

Joined: Tue Jul 25, 2006 9:05 am
Posts: 163
Location: Stuttgart/Karlsruhe, Germany
Hibernate version: Version with JBoss 4.0.4GA

Mapping documents: None, annotations being used

Name and version of the database you are using: Postgres 8.1.4

I have set up a very simple entity model to test relationships in EJB3. I have no problems in adding to them, and removing an entity so long as it is not used in a relationship (not refered to as a foriegn key value).

The problem occurs when trying to remove the entity when it is used in a relationship be it many to many or many to one/one to many. To give an example of a this in a many to many context.

I have 3 Entities Student, Course, and Institution and all of these relationships are many to many (please see below).
In Student.java the student-course many to many
Code:
@ManyToMany(mappedBy = "students", fetch = FetchType.EAGER, cascade ={CascadeType.PERSIST, CascadeType.MERGE})   
    public Collection<Course> getCourses() {
        return mCourses;
    }


in Course.java student-course many to many
Code:
@ManyToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE}, fetch = FetchType.EAGER)
    @JoinTable(name = "course_student",
                joinColumns = @JoinColumn(name = "course_id", referencedColumnName = "course_id"),
                inverseJoinColumns = @JoinColumn(name = "student_id", referencedColumnName = "student_id"))   
    public Set<Student> getStudents() {       
        return mEnrolledStudents;
    }


in Course.java course-institution many to many
Code:
@ManyToMany(mappedBy = "courses", cascade = {CascadeType.PERSIST, CascadeType.MERGE})
    public Collection<Institution> getInstitutions() {
        return mInstitutionsRunningCourse;
    }


in Institution.java course-institution many to many
Code:
@ManyToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE}, fetch = FetchType.EAGER)   
    @JoinTable(name = "institution_course",
                joinColumns = @JoinColumn(name = "institution", referencedColumnName = "institution_id") ,
                inverseJoinColumns = @JoinColumn(name = "course", referencedColumnName = "course_id") )
    public Set<Course> getCourses() {       
        return mCoursesOffered;
    }


I create a course with 3 students on it (3 entries in the association table) and also link this course to 2 institutions (2 entries in the association table). Then i try to call the EntityManager.remove() method from within the same SLSB. The errors i get is this
Quote:
Caused by: java.lang.RuntimeException: org.jboss.tm.JBossRollbackException: Unable to commit, tx=TransactionImpl:XidImpl[FormatId=257, GlobalId=dale.test.de/50, BranchQual=, localId=50] status=STATUS_NO_TRANSACTION; - nested throwable: (javax.persistence.EntityNotFoundException: org.hibernate.ObjectDeletedException: deleted entity passed to persist: [test.Course#<null>])
at org.jboss.aspects.tx.TxPolicy.handleEndTransactionException(TxPolicy.java:198)
at org.jboss.aspects.tx.TxPolicy.endTransaction(TxPolicy.java:180)
at org.jboss.aspects.tx.TxPolicy.invokeInOurTx(TxPolicy.java:87)
at org.jboss.aspects.tx.TxInterceptor$Required.invoke(TxInterceptor.java:197)
at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
at org.jboss.aspects.tx.TxPropagationInterceptor.invoke(TxPropagationInterceptor.java:76)
at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
at org.jboss.ejb3.stateless.StatelessInstanceInterceptor.invoke(StatelessInstanceInterceptor.java:62)
at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
at org.jboss.aspects.security.AuthenticationInterceptor.invoke(AuthenticationInterceptor.java:78)
at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
at org.jboss.ejb3.ENCPropagationInterceptor.invoke(ENCPropagationInterceptor.java:47)
at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
at org.jboss.ejb3.asynchronous.AsynchronousInterceptor.invoke(AsynchronousInterceptor.java:106)
at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
at org.jboss.ejb3.stateless.StatelessContainer.localInvoke(StatelessContainer.java:181)
at org.jboss.ejb3.stateless.StatelessLocalProxy.invoke(StatelessLocalProxy.java:79)
at $Proxy347.tryDelete(Unknown Source)
at test.TestRelationships.storeData(TestRelationships.java:63)
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 org.jboss.mx.interceptor.ReflectedDispatcher.invoke(ReflectedDispatcher.java:155)
... 29 more
Caused by: org.jboss.tm.JBossRollbackException: Unable to commit, tx=TransactionImpl:XidImpl[FormatId=257, GlobalId=dale.test.de/50, BranchQual=, localId=50] status=STATUS_NO_TRANSACTION; - nested throwable: (javax.persistence.EntityNotFoundException: org.hibernate.ObjectDeletedException: deleted entity passed to persist: [test.Course#<null>])
at org.jboss.tm.TransactionImpl.commit(TransactionImpl.java:372)
at org.jboss.tm.TxManager.commit(TxManager.java:240)
at org.jboss.aspects.tx.TxPolicy.endTransaction(TxPolicy.java:175)
... 51 more
Caused by: javax.persistence.EntityNotFoundException: org.hibernate.ObjectDeletedException: deleted entity passed to persist: [test.Course#<null>]
at org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:564)
at org.hibernate.ejb.AbstractEntityManagerImpl$1.beforeCompletion(AbstractEntityManagerImpl.java:484)
at org.jboss.tm.TransactionImpl.doBeforeCompletion(TransactionImpl.java:1491)
at org.jboss.tm.TransactionImpl.beforePrepare(TransactionImpl.java:1110)
at org.jboss.tm.TransactionImpl.commit(TransactionImpl.java:324)
... 53 more
Caused by: org.hibernate.ObjectDeletedException: deleted entity passed to persist: [test.Course#<null>]
at org.hibernate.event.def.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:90)
at org.hibernate.impl.SessionImpl.firePersistOnFlush(SessionImpl.java:646)
at org.hibernate.impl.SessionImpl.persistOnFlush(SessionImpl.java:638)
at org.hibernate.engine.CascadingAction$9.cascade(CascadingAction.java:225)
at org.hibernate.engine.Cascade.cascadeToOne(Cascade.java:213)
at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:157)
at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:108)
at org.hibernate.engine.Cascade.cascadeCollectionElements(Cascade.java:290)
at org.hibernate.engine.Cascade.cascadeCollection(Cascade.java:185)
at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:160)
at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:108)
at org.hibernate.engine.Cascade.cascade(Cascade.java:248)
at org.hibernate.event.def.AbstractFlushingEventListener.cascadeOnFlush(AbstractFlushingEventListener.java:130)
at org.hibernate.event.def.AbstractFlushingEventListener.prepareEntityFlushes(AbstractFlushingEventListener.java:121)
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:993)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:340)
at org.hibernate.ejb.AbstractEntityManagerImpl$1.beforeCompletion(AbstractEntityManagerImpl.java:475)
... 56 more


I cannot figure out what is wrong, I even tried to clear the set and collection first thus leaving the database in the correct state then removing it and it is still the same error. I have also tried annotating with this :

Code:
@org.hibernate.annotations.Cascade( {
         org.hibernate.annotations.CascadeType.ALL,
         org.hibernate.annotations.CascadeType.DELETE_ORPHAN })


but that does also not work.

Can anyone shed any light on where i have gone wrong ?

Thanks in advance,

Andy


Last edited by andydale on Wed Feb 14, 2007 10:54 am, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 18, 2006 9:50 am 
Senior
Senior

Joined: Tue Jul 25, 2006 9:05 am
Posts: 163
Location: Stuttgart/Karlsruhe, Germany
I managed to fix this problem, i was removing the students from the course (clearing the students Set in course.java) and should of been removing the course (collection.remove(course) in student.java)from the students.

Thanks,

Andy


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 14, 2007 7:12 am 
Newbie

Joined: Wed Feb 14, 2007 7:00 am
Posts: 1
I had similar problem. Before removing record i'm making
Code:
students = this.entityManager.find(Students.class, students.getId());
this.entityManager.remove(entityManager.merge(students));


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

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.