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