protected void verifyCascadedMergeAsPersist(Boolean loadLazy)
throws MalformedURLException, SecurityException, IllegalArgumentException, ClassNotFoundException,
NoSuchMethodException, IllegalAccessException {
EntityTransaction tx = em.getTransaction();
PersonEntity personEntity = new PersonEntity();
personEntity.setName(personAName);
PhoneEntity phoneEntity = JPATestUtils.createPhoneEntity(oldPhoneName);
LinkedList<PhoneEntity> phoneEntities = new LinkedList<PhoneEntity>();
tx.begin();
em.persist(personEntity);
phoneEntities.add(phoneEntity);
personEntity.setPhoneList(phoneEntities);
phoneEntity.setPhoneOwner(personEntity);
em.persist(phoneEntity);
tx.commit();
tx.begin();
personEntity = em.find(PersonEntity.class, personEntity.getId());
if (loadLazy) {
personEntity.getPhoneList();
}
tx.commit();
Assert.assertEquals(personAName, personEntity.getName());
PhoneEntity newPhone = JPATestUtils.createPhoneEntity(newPhoneName);
newPhone.setPhoneOwner(personEntity);
if (loadLazy) {
Assert.assertNotNull(personEntity.getPhoneList());
personEntity.getPhoneList().add(newPhone);
} else {
JPATestUtils.assertDetachedFieldException(personEntity, "getPhoneList");
phoneEntities = new LinkedList<PhoneEntity>();
phoneEntities.add(newPhone);
personEntity.setPhoneList(phoneEntities);
}
tx.begin();
if (loadLazy) {
em.merge(personEntity);
} else {
em.persist(newPhone);
}
tx.commit();
tx.begin();
personEntity = em.find(PersonEntity.class, personEntity.getId());
Assert.assertNotNull(personEntity.getPhoneList());
Assert.assertEquals(personEntity.getPhoneList().size(), 2);
JPATestUtils.verifyContains(personEntity.getPhoneList(), new String[] {newPhoneName, oldPhoneName});
tx.commit();
}