|
Orphan removal does not work properly when an element is added to a collection while detached in the following:
Session session = openSession();
Transaction t = session.beginTransaction();
Product prod = new Product();
prod.setName("Widget");
session.persist(prod);
t.commit();
session.close();
Part part = new Part();
part.setName("Widge");
part.setDescription("part if a Widget");
prod.getParts().add(part);
session = openSession();
t = session.beginTransaction();
session.merge(prod);
prod.getParts().remove(part);
session.merge( prod );
t.commit();
session.close();
The next time prod is loaded from the database, part is still in the collection.
The following works properly when the element was added while the entity was attached to the session:
Session session = openSession();
Transaction t = session.beginTransaction();
Product prod = new Product();
prod.setName("Widget");
Part part = new Part();
part.setName("Widge");
part.setDescription("part if a Widget");
prod.getParts().add(part);
session.persist(prod);
t.commit();
session.close();
session = openSession();
t = session.beginTransaction();
session.merge(prod);
prod.getParts().remove(part);
session.merge( prod );
t.commit();
session.close();
|