public void testMergeOfLazyFields() {
EntityManager em = emf.createEntityManager();
try {
em.getTransaction().begin();
Outer o1 = new Outer();
Inner i1 = new Inner();
o1.setInners(new ArrayList<Inner>());
o1.getInners().add(i1);
em.persist(o1);
em.getTransaction().commit();
em.clear(); // the objects will now get detached.
long id = o1.getId();
em.getTransaction().begin();
Outer o2 = em.find(Outer.class, id);
// Since o2 is in the context, it should be ignored... but the merge will needs to be cascaded
// to loaded fields.
Outer mergedO2 = em.merge(o2);
// Make sure that the merge operation didn't return a different outer.
assertEquals(mergedO2, o2);
em.getTransaction().commit();
em.clear();
// Fetch again
em.getTransaction().begin();
Outer o3 = em.find(Outer.class, id);
// We're checking that the merge didn't cascade to the unloaded field and wipe out all Inners
assertTrue(o3.getInners().size() > 0);
em.getTransaction().commit();
em.clear();
} finally {