Hi,
I have a doubt whether JPA's orphanRemoval attribute works correctly in Hibernate (version 3.6.7) or not.
I have two simple entities "Parent" and "Child" (getters, setters, equals and hashCode are omitted for brevity):
Code:
@Entity
public class Parent
{
@Id
@GeneratedValue
private int id;
@OneToMany(mappedBy="parent", orphanRemoval=true)
private List<Child> children = new LinkedList<Child>();
}
@Entity
public class Child
{
@Id
@GeneratedValue
private int id;
@ManyToOne
private Parent parent;
}
Here is my test code:
Code:
EntityManagerFactory emf = Persistence.createEntityManagerFactory("TestPersistenceUnit");
EntityManager em = emf.createEntityManager();
// relate and persist one parent and one child entity
em.getTransaction().begin();
Parent parent = new Parent();
Child child = new Child();
child.setParent(parent);
parent.getChildren().add(child);
em.persist(parent);
em.persist(child);
em.getTransaction().commit();
// disconnect their relationship
em = emf.createEntityManager();
em.getTransaction().begin();
parent = em.find(Parent.class, 1);
parent.getChildren().get(0).setParent(null);
parent.getChildren().clear();
em.getTransaction().commit();
According to the JPA 2.0 specification, the orphaned "Child" entity should be removed from the database. However, this is not the case and it remains in the database with its foreign key set to NULL.
Is this a bug in Hibernate or am I missing something?
Thanks!