try {
Car c1 = new Car();
Car c2 = new Car();
Make mk1 = new Make();
ArrayList<Model> models = new ArrayList<Model>();
Model md1 = new Model();
models.add(md1);
Model md2 = new Model();
models.add(md2);
//populate bidirectional relationships
c1.setModel(md1);
c2.setModel(md2);
md1.setCar(c1);
md1.setMake(mk1);
md2.setCar(c2);
md1.setMake(mk1);
mk1.setModels(models);
// Persist car1 - will cascade
em.getTransaction().begin();
em.persist(c1);
em.getTransaction().commit();
Object[] entities = new Object[] { c1, c2, mk1, md1, md2 };
// detach all
em.clear();
// verify all entities are detached and references to them
// are also detached.
verifyDetached(em, entities);
em.getTransaction().begin();
// Merge model back in and verify all entities are newly merged entities
Model mergedModel = em.merge(md1);
assertFalse(mergedModel == md1);
assertFalse(mergedModel.getMake() == mk1);
List<Model> mds = mergedModel.getMake().getModels();
assertTrue(mds.contains(mergedModel));
assertFalse(c1 == mergedModel.getCar());
assertTrue(mergedModel.getCar().getModel() == mergedModel);
em.remove(mergedModel);
em.getTransaction().commit();
} finally {
if (em != null) {
if (em.getTransaction().isActive())