// modify detached entity: add a new master
@Test
  public void testCascadeMergeWithNewEntity() {
    setUp();
    EntityMasterCascade master = new EntityMasterCascade();
    master.setDescription("Master version 1");
    entityManager.persist(master);
    Integer masterId = master.getPersistenceId();
    EntityDetailCascade detail = new EntityDetailCascade();
    detail.setMaster(master);
    detail.setDescription("Detail version 1");
    entityManager.persist(detail);
    Integer detailId = detail.getPersistenceId();
    tearDown();
    // retrieve persistence versions
    Integer masterVersion = master.getPersistenceVersion();
    Integer detailVersion = detail.getPersistenceVersion();
    // modify detached entity: add a new master
    assertEquals(master, detail.getMaster());
    detail.setDescription("Detail version 2");
    EntityMasterCascade newMaster = new EntityMasterCascade();
    newMaster.setDescription("New Master version 1");
    detail.setMaster(newMaster);
    setUp();
    // MUDO: this persist is only needed because cascading is not working
    // for a new entity when merge is used
    entityManager.persist(newMaster);
    // get a managed copy from the merge operation
    detail = entityManager.merge(detail);
    Integer newMasterId = detail.getMaster().getPersistenceId();
    tearDown();
    // verification
    setUp();
    master = entityManager.find(EntityMasterCascade.class, masterId);
    detail = entityManager.find(EntityDetailCascade.class, detailId);
    newMaster = entityManager.find(EntityMasterCascade.class, newMasterId);
    assertTrue(masterVersion.compareTo(master.getPersistenceVersion())==0);
    assertTrue(detailVersion.compareTo(detail.getPersistenceVersion())<0);
    assertEquals(newMaster, detail.getMaster());
    assertEquals(1, newMaster.getDetails().size());
    assertEquals(0, master.getDetails().size());
    for (EntityDetailCascade d : newMaster.getDetails()) {
      assertEquals(detail, d);
    }
    tearDown();
  }
Integer masterVersion = master.getPersistenceVersion();
Integer detailVersion = detail.getPersistenceVersion();
// modify detached entity: add a new master
assertEquals(master, detail.getMaster());
detail.setDescription("Detail version 2");
Integer masterId = master.getPersistenceId();
EntityDetailCascade detail = new EntityDetailCascade();
detail.setMaster(master);
detail.setDescription("Detail version 1");
entityManager.persist(detail);
Integer detailId = detail.getPersistenceId();
tearDown();
// retrieve persistence versions
Integer masterVersion = master.getPersistenceVersion();
Integer detailVersion = detail.getPersistenceVersion();
// modify detached entity: add a new master
assertEquals(master, detail.getMaster());
detail.setDescription("Detail version 2");
EntityMasterCascade newMaster = new EntityMasterCascade();
newMaster.setDescription("New Master version 1");
detail.setMaster(newMaster);
setUp();
// MUDO: this persist is only needed because cascading is not working
// for a new entity when merge is used
entityManager.persist(newMaster);
// get a managed copy from the merge operation
@Test
public void testCascadeMergeWithNewEntity() {
setUp();
EntityMasterCascade master = new EntityMasterCascade();
master.setDescription("Master version 1");
entityManager.persist(master);
Integer masterId = master.getPersistenceId();
EntityDetailCascade detail = new EntityDetailCascade();
detail.setMaster(master);
detail.setDescription("Detail version 1");
entityManager.persist(detail);
Integer detailId = detail.getPersistenceId();
tearDown();
// retrieve persistence versions
Integer masterVersion = master.getPersistenceVersion();
Integer detailVersion = detail.getPersistenceVersion();
// modify detached entity: add a new master
assertEquals(master, detail.getMaster());
detail.setDescription("Detail version 2");
EntityMasterCascade newMaster = new EntityMasterCascade();
newMaster.setDescription("New Master version 1");
detail.setMaster(newMaster);
setUp();
// MUDO: this persist is only needed because cascading is not working
// for a new entity when merge is used
entityManager.persist(newMaster);
// get a managed copy from the merge operation
detail = entityManager.merge(detail);
Integer newMasterId = detail.getMaster().getPersistenceId();
tearDown();
// verification
setUp();
master = entityManager.find(EntityMasterCascade.class, masterId);
detail = entityManager.find(EntityDetailCascade.class, detailId);
newMaster = entityManager.find(EntityMasterCascade.class, newMasterId);
assertTrue(masterVersion.compareTo(master.getPersistenceVersion())==0);
assertTrue(detailVersion.compareTo(detail.getPersistenceVersion())<0);