Hi,
I'm trying to use Hibernate's "delete orphan" feature and it keeps failing in my application. Objects that I removed from the parent's child collection are not deleted when persisting the parent object.
Therefore I checked the hibernate source code for similiar tests and found a test in the Hibernate Annotation package org.hibernate.test.annotations.onetomany.OneToMany.testCascadeDeleteOrphan(). I changed this test so that it uses the EntityManager instead of the HibernateSession and it is now failing too.
Here is the code I tried:
Code:
public void testCascadeDeleteOrphan() throws Exception
{
EntityManagerFactory emf = Persistence.createEntityManagerFactory("manager1");
EntityTransaction tx;
EntityManager em = emf.createEntityManager();
tx = em.getTransaction();
tx.begin();
Troop disney = new Troop();
disney.setName("Disney");
Soldier mickey = new Soldier();
mickey.setName("Mickey");
disney.addSoldier(mickey);
em.persist(disney);
tx.commit();
em.close();
em = emf.createEntityManager();
tx = em.getTransaction();
tx.begin();
Troop troop = em.find(Troop.class, disney.getId());
Soldier soldier = troop.getSoldiers().iterator().next();
troop.getSoldiers().remove(soldier);
tx.commit();
em.close();
em = emf.createEntityManager();
tx = em.getTransaction();
tx.begin();
soldier = em.find(Soldier.class, mickey.getId());
assertNull("delete-orphan should work", soldier);
troop = em.find(Troop.class, disney.getId());
em.remove(troop);
tx.commit();
em.close();
}
Ideas and comments are welcome.
Regards,
Johan