-->
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.  [ 2 posts ] 
Author Message
 Post subject: (JPA) Correct way to remove entity ?
PostPosted: Tue Jun 22, 2010 9:28 am 
Regular
Regular

Joined: Thu Oct 19, 2006 12:07 pm
Posts: 75
Hi!

In a project a bug appeared after some "unrelated" changes and we had to add a line of code to fix it.

We run some EJBs on JBoss 4.2.3.GA, using JPA powered by hibernate 3.2.4.sp1

Old code:
Code:
 
public void removeFoo(long fooId) { // is an EJB method
       Foo f = entityManager.find(Foo.class, fooId);
       entityManager.remove(f);
}


New code:
Code:
   
public void removeFoo(long fooId) { // is an EJB method
       Foo f = entityManager.find(Foo.class, fooId);
       f.getBar().getFoos().remove(f); // new line added to fix problem
       entityManager.remove(f);
}


Mappings:
Code:

@Entity
@Table( name = "TBL_FOO" )
public class Foo implements Serializable
{

   @ManyToOne( fetch = FetchType.EAGER )
   @JoinColumn( name = "BAR_ID", nullable = false, insertable = true, updatable = true )
   private Bar bar;
// rest snipped
}

// in other file:


@Entity
@Table( name = "TBL_BAR" )
public class Bar implements Serializable
{
   @OneToMany( fetch = FetchType.EAGER, cascade = CascadeType.ALL, mappedBy = "bar" )
   private Set<Foo> foos = new HashSet<Foo>();
// rest snipped
}


The change that I suspect triggered the problem is that change from FetchType.LAZY to FetchType.EAGER in Bar.java.
We got this exception after removeFoo() returns:
Code:
javax.persistence.EntityNotFoundException: deleted entity passed to persist: [com.acme.Foo#<null>]
        at org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:613)
        at org.hibernate.ejb.AbstractEntityManagerImpl$1.beforeCompletion(AbstractEntityManagerImpl.java:524)
        at com.arjuna.ats.internal.jta.resources.arjunacore.SynchronizationImple.beforeCompletion(SynchronizationImple.java:114)
(rest in com.arjuna.. and org.jboss... not very informative)



This is run under JBoss 4.2.3.GA on Windows XP Pro SP3 with Sun Java 1.6.0u20


So the question is:
Is this the correct way to remove entities? That is manually removing references to it from other entities?
Sound like a lot of "unneeded" work. After all the objects are "managed", right?

Regards,
David


Top
 Profile  
 
 Post subject: Re: (JPA) Correct way to remove entity ?
PostPosted: Thu Sep 30, 2010 3:27 pm 
Regular
Regular

Joined: Thu Oct 19, 2006 12:07 pm
Posts: 75
I guess part of the cause is that the relation Bar.foos is not inverse, so at the session end foo still has a reference to the deleted bar and hibernate tries to save that to DB, but fails, as the bar is already (marked to be) deleted in DB.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.