@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();
  }
@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();
   
    Integer masterVersion = master.getPersistenceVersion();
    Integer detailVersion = detail.getPersistenceVersion();
   
    assertEquals(master, detail.getMaster());
    detail.setDescription("Detail version 2");
    EntityMasterCascade newMaster = new EntityMasterCascade();
    newMaster.setDescription("New Master version 1");
    detail.setMaster(newMaster);
    setUp();
   
   
    entityManager.persist(newMaster);
   
    detail = entityManager.merge(detail);
    Integer newMasterId = detail.getMaster().getPersistenceId();
    tearDown();
   
    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();
  }
@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(); integer masterversion = master.getpersistenceversion(); integer detailversion = detail.getpersistenceversion(); assertequals(master, detail.getmaster()); detail.setdescription("detail version 2"); entitymastercascade newmaster = new entitymastercascade(); newmaster.setdescription("new master version 1"); detail.setmaster(newmaster); setup(); entitymanager.persist(newmaster); detail = entitymanager.merge(detail); integer newmasterid = detail.getmaster().getpersistenceid(); teardown(); 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(); }