Hi.
My problem is that the @preRemove and @postRemove functions are not called when the entity is removed because of the "orphanRemoval = true" parameter in a relation.
I think this is a bug, but in the jpa spec i haven't found a statement that actually states that the life cycle callbacks must be called after orphan removal.
So I'd like to know if i should file a bug on this?
It is quite simple to test with two classes and a simple test case. I added a maven project containing everything needed. Changing the jpa provider in the pom.xml (dependency is just commented out) shows that the behavior is different with openjpa and hibernate.
Here the most important parts to demonstrate the problem.
@Entity
public class A {
@Id
private int id;
@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
private List<B> bs = new ArrayList<B>();
public A() {}
public A(int id) {
this.id = id;
}
public List<B> getBs() {
return bs;
}
}
@Entity
public class B {
@Id
private int id;
@Transient
private boolean preRemoveCalled = false;
@PreRemove
public void prepareRemove() {
preRemoveCalled = true;
}
public B() {}
public B(int id) {
this.id = id;
}
public boolean isPreRemoveCalled() {
return preRemoveCalled;
}
}
@Test
public void testWithOrphanRemoal() {
EntityTransaction et = em.getTransaction();
et.begin();
try {
A a1 = new A(1);
B b1 = new B(1);
a1.getBs().add(b1);
em.persist(a1);
em.flush();
em.refresh(a1);
em.refresh(b1);
a1.getBs().remove(b1);
em.flush();
// This fails with eclipseLink but works with openjpa and hibernate
Assert.assertTrue(b1.isPreRemoveCalled());
} finally {
et.rollback();
}
}