EntityManager em = emf.createEntityManager();
try {
// Create simple bi-di graph
ShipPackage p = new ShipPackage();
Label l = new Label(p);
p.setLabel(l);
// Persist
em.getTransaction().begin();
em.persist(l);
em.getTransaction().commit();
// Detach
em.clear();
assertFalse(em.contains(l));
assertFalse(em.contains(p));
assertFalse(em.contains(l.getPackage()));
assertFalse(em.contains(p.getLabel()));
em.getTransaction().begin();
Label mergedLabel = em.merge(l);
assertFalse(mergedLabel == l);
assertFalse(p == mergedLabel.getPackage());
// Assert that the bi-directional relationship points to the
// newly merged entity
assertTrue(mergedLabel == mergedLabel.getPackage().getLabel());
assertFalse(l == mergedLabel.getPackage().getLabel());
em.remove(mergedLabel);
em.getTransaction().commit();
} finally {
if (em != null) {
if (em.getTransaction().isActive())