Program: 68822
Project: 68822
Buggy source:
protected void internalTestManagedEmployeesMassInsertOrMerge(boolean shouldInsert,boolean shouldUseSequencing) throws Exception {
  int nLevels=2;
  int nDirects=50;
  int id=0;
  EntityManager em=null;
  if (!shouldUseSequencing) {
    Employee emp=new Employee();
    em=createEntityManager();
    em.getTransaction().begin();
    em.persist(emp);
    id=emp.getId();
    em.getTransaction().rollback();
    em.close();
  }
  Employee topEmployee=new Employee();
  topEmployee.setFirstName("String_Node_Str");
  topEmployee.setLastName("String_Node_Str");
  if (!shouldUseSequencing) {
    topEmployee.setId(id++);
  }
  ArrayList<Employee> employeesForHigherLevel=new ArrayList<Employee>(1);
  ArrayList<Employee> employeesForCurrentLevel;
  employeesForHigherLevel.add(topEmployee);
  int nEmployeesTotal=1;
  for (int nLevel=1; nLevel <= nLevels; nLevel++) {
    employeesForCurrentLevel=new ArrayList<Employee>(employeesForHigherLevel.size() * nDirects);
    Iterator<Employee> it=employeesForHigherLevel.iterator();
    while (it.hasNext()) {
      Employee mgr=it.next();
      for (int nCurrent=0; nCurrent < nDirects; nCurrent++) {
        Employee employee=new Employee();
        employee.setFirstName("String_Node_Str" + nLevel);
        employee.setLastName("String_Node_Str" + employeesForCurrentLevel.size());
        if (!shouldUseSequencing) {
          employee.setId(id++);
        }
        employeesForCurrentLevel.add(employee);
        mgr.addManagedEmployee(employee);
      }
    }
    employeesForHigherLevel=employeesForCurrentLevel;
    nEmployeesTotal=nEmployeesTotal + employeesForCurrentLevel.size();
  }
  em=createEntityManager();
  em.getTransaction().begin();
  try {
    if (shouldInsert) {
      em.persist(topEmployee);
    }
 else {
      em.merge(topEmployee);
    }
    em.getTransaction().commit();
  }
  finally {
    if (em.getTransaction().isActive()) {
      em.getTransaction().rollback();
    }
    em.close();
  }
  em=createEntityManager();
  em.getTransaction().begin();
  try {
    em.createQuery("String_Node_Str").executeUpdate();
    em.getTransaction().commit();
  }
  finally {
    if (em.getTransaction().isActive()) {
      em.getTransaction().rollback();
    }
    ((EntityManagerImpl)em).getServerSession().getIdentityMapAccessor().initializeAllIdentityMaps();
    em.close();
  }
}

Fixed version (ground truth):
protected void internalTestManagedEmployeesMassInsertOrMerge(boolean shouldInsert,boolean shouldUseSequencing) throws Exception {
  int nLevels=2;
  int nDirects=50;
  int id=0;
  EntityManager em=null;
  if (!shouldUseSequencing) {
    Employee emp=new Employee();
    em=createEntityManager();
    em.getTransaction().begin();
    em.persist(emp);
    id=emp.getId();
    em.getTransaction().rollback();
    em.close();
  }
  Employee topEmployee=new Employee();
  topEmployee.setFirstName("String_Node_Str");
  topEmployee.setLastName("String_Node_Str");
  if (!shouldUseSequencing) {
    topEmployee.setId(id++);
  }
  ArrayList<Employee> employeesForHigherLevel=new ArrayList<Employee>(1);
  ArrayList<Employee> employeesForCurrentLevel;
  employeesForHigherLevel.add(topEmployee);
  int nEmployeesTotal=1;
  for (int nLevel=1; nLevel <= nLevels; nLevel++) {
    employeesForCurrentLevel=new ArrayList<Employee>(employeesForHigherLevel.size() * nDirects);
    Iterator<Employee> it=employeesForHigherLevel.iterator();
    while (it.hasNext()) {
      Employee mgr=it.next();
      for (int nCurrent=0; nCurrent < nDirects; nCurrent++) {
        Employee employee=new Employee();
        employee.setFirstName("String_Node_Str" + nLevel);
        employee.setLastName("String_Node_Str" + employeesForCurrentLevel.size());
        if (!shouldUseSequencing) {
          employee.setId(id++);
        }
        employeesForCurrentLevel.add(employee);
        mgr.addManagedEmployee(employee);
      }
    }
    employeesForHigherLevel=employeesForCurrentLevel;
    nEmployeesTotal=nEmployeesTotal + employeesForCurrentLevel.size();
  }
  em=createEntityManager();
  em.getTransaction().begin();
  try {
    if (shouldInsert) {
      em.persist(topEmployee);
    }
 else {
      em.merge(topEmployee);
    }
    em.getTransaction().commit();
  }
  finally {
    if (em.getTransaction().isActive()) {
      em.getTransaction().rollback();
    }
    em.close();
  }
  em=createEntityManager();
  em.getTransaction().begin();
  try {
    List<Employee> employees=em.createQuery("String_Node_Str").getResultList();
    Iterator<Employee> i=employees.iterator();
    while (i.hasNext()) {
      Employee emp=i.next();
      emp.setManager(null);
      emp.setManagedEmployees(null);
      em.remove(emp);
    }
    em.getTransaction().commit();
  }
  finally {
    if (em.getTransaction().isActive()) {
      em.getTransaction().rollback();
    }
    ((EntityManagerImpl)em).getServerSession().getIdentityMapAccessor().initializeAllIdentityMaps();
    em.close();
  }
}