Program: 68829
Project: 68829
Buggy source:
public void testClearWithFlush2(){
  String firstName="String_Node_Str";
  EntityManager em=createEntityManager();
  int employee_1_NotInCache_id=0;
  int employee_2_NotInCache_id=0;
  int manager_NotInCache_id=0;
  em.getTransaction().begin();
  try {
    Employee employee_1_NotInCache=new Employee();
    employee_1_NotInCache.setFirstName(firstName);
    employee_1_NotInCache.setLastName("String_Node_Str");
    Employee employee_2_NotInCache=new Employee();
    employee_2_NotInCache.setFirstName(firstName);
    employee_2_NotInCache.setLastName("String_Node_Str");
    Employee manager_NotInCache=new Employee();
    manager_NotInCache.setFirstName(firstName);
    manager_NotInCache.setLastName("String_Node_Str");
    manager_NotInCache.addManagedEmployee(employee_1_NotInCache);
    em.persist(manager_NotInCache);
    em.persist(employee_1_NotInCache);
    em.persist(employee_2_NotInCache);
    em.getTransaction().commit();
    employee_1_NotInCache_id=employee_1_NotInCache.getId();
    employee_2_NotInCache_id=employee_2_NotInCache.getId();
    manager_NotInCache_id=manager_NotInCache.getId();
  }
 catch (  RuntimeException ex) {
    if (em.getTransaction().isActive()) {
      em.getTransaction().rollback();
    }
    em.close();
    throw ex;
  }
  clearCache();
  em=createEntityManager();
  int employee_1_InCache_id=0;
  int employee_2_InCache_id=0;
  int manager_InCache_id=0;
  em.getTransaction().begin();
  try {
    Employee employee_1_InCache=new Employee();
    employee_1_InCache.setFirstName(firstName);
    employee_1_InCache.setLastName("String_Node_Str");
    Employee employee_2_InCache=new Employee();
    employee_2_InCache.setFirstName(firstName);
    employee_2_InCache.setLastName("String_Node_Str");
    Employee manager_InCache=new Employee();
    manager_InCache.setFirstName(firstName);
    manager_InCache.setLastName("String_Node_Str");
    manager_InCache.addManagedEmployee(employee_1_InCache);
    em.persist(manager_InCache);
    em.persist(employee_1_InCache);
    em.persist(employee_2_InCache);
    em.getTransaction().commit();
    employee_1_InCache_id=employee_1_InCache.getId();
    employee_2_InCache_id=employee_2_InCache.getId();
    manager_InCache_id=manager_InCache.getId();
  }
 catch (  RuntimeException ex) {
    if (em.getTransaction().isActive()) {
      em.getTransaction().rollback();
    }
    em.close();
    throw ex;
  }
  em=createEntityManager();
  int employee_1_New_id=0;
  int employee_2_New_id=0;
  int employee_3_New_id=0;
  int employee_4_New_id=0;
  int manager_New_id=0;
  em.getTransaction().begin();
  try {
    Employee employee_1_New=new Employee();
    employee_1_New.setFirstName(firstName);
    employee_1_New.setLastName("String_Node_Str");
    em.persist(employee_1_New);
    employee_1_New_id=employee_1_New.getId();
    Employee employee_2_New=new Employee();
    employee_2_New.setFirstName(firstName);
    employee_2_New.setLastName("String_Node_Str");
    em.persist(employee_2_New);
    employee_2_New_id=employee_2_New.getId();
    Employee employee_3_New=new Employee();
    employee_3_New.setFirstName(firstName);
    employee_3_New.setLastName("String_Node_Str");
    em.persist(employee_3_New);
    employee_3_New_id=employee_3_New.getId();
    Employee employee_4_New=new Employee();
    employee_4_New.setFirstName(firstName);
    employee_4_New.setLastName("String_Node_Str");
    em.persist(employee_4_New);
    employee_4_New_id=employee_4_New.getId();
    Employee manager_New=new Employee();
    manager_New.setFirstName(firstName);
    manager_New.setLastName("String_Node_Str");
    em.persist(manager_New);
    manager_New_id=manager_New.getId();
    Employee employee_1_NotInCache=em.find(Employee.class,employee_1_NotInCache_id);
    employee_1_NotInCache.setLastName(employee_1_NotInCache.getLastName() + "String_Node_Str");
    Employee employee_2_NotInCache=em.find(Employee.class,employee_2_NotInCache_id);
    employee_2_NotInCache.setLastName(employee_2_NotInCache.getLastName() + "String_Node_Str");
    Employee manager_NotInCache=em.find(Employee.class,manager_NotInCache_id);
    manager_NotInCache.setLastName(manager_NotInCache.getLastName() + "String_Node_Str");
    Employee employee_1_InCache=em.find(Employee.class,employee_1_InCache_id);
    employee_1_InCache.setLastName(employee_1_InCache.getLastName() + "String_Node_Str");
    Employee employee_2_InCache=em.find(Employee.class,employee_2_InCache_id);
    employee_2_InCache.setLastName(employee_2_InCache.getLastName() + "String_Node_Str");
    Employee manager_InCache=em.find(Employee.class,manager_InCache_id);
    manager_InCache.setLastName(manager_InCache.getLastName() + "String_Node_Str");
    manager_NotInCache.addManagedEmployee(employee_1_New);
    manager_InCache.addManagedEmployee(employee_2_New);
    manager_New.addManagedEmployee(employee_3_New);
    manager_New.addManagedEmployee(employee_2_NotInCache);
    manager_New.addManagedEmployee(employee_2_InCache);
    em.flush();
    em.clear();
    em.getTransaction().commit();
  }
 catch (  RuntimeException ex) {
    if (em.getTransaction().isActive()) {
      em.getTransaction().rollback();
    }
    em.close();
    throw ex;
  }
  String errorMsg="String_Node_Str";
  em=createEntityManager();
  Employee manager_NotInCache=em.find(Employee.class,manager_NotInCache_id);
  if (!manager_NotInCache.getLastName().endsWith("String_Node_Str")) {
    errorMsg=errorMsg + "String_Node_Str";
  }
  Iterator it=manager_NotInCache.getManagedEmployees().iterator();
  while (it.hasNext()) {
    Employee emp=(Employee)it.next();
    if (emp.getId() == employee_1_NotInCache_id) {
      if (!emp.getLastName().endsWith("String_Node_Str")) {
        errorMsg=errorMsg + "String_Node_Str";
      }
    }
 else     if (emp.getId() == employee_1_New_id) {
      if (!emp.getLastName().endsWith("String_Node_Str")) {
        errorMsg=errorMsg + "String_Node_Str";
      }
    }
 else {
      errorMsg=errorMsg + "String_Node_Str" + emp.getLastName();
    }
  }
  if (manager_NotInCache.getManagedEmployees().size() != 2) {
    errorMsg=errorMsg + "String_Node_Str" + manager_NotInCache.getManagedEmployees().size();
  }
  Employee manager_InCache=em.find(Employee.class,manager_InCache_id);
  if (!manager_InCache.getLastName().endsWith("String_Node_Str")) {
    errorMsg=errorMsg + "String_Node_Str";
  }
  it=manager_InCache.getManagedEmployees().iterator();
  while (it.hasNext()) {
    Employee emp=(Employee)it.next();
    if (emp.getId() == employee_1_InCache_id) {
      if (!emp.getLastName().endsWith("String_Node_Str")) {
        errorMsg=errorMsg + "String_Node_Str";
      }
    }
 else     if (emp.getId() == employee_2_New_id) {
      if (!emp.getLastName().endsWith("String_Node_Str")) {
        errorMsg=errorMsg + "String_Node_Str";
      }
    }
 else {
      errorMsg=errorMsg + "String_Node_Str" + emp.getLastName();
    }
  }
  if (manager_InCache.getManagedEmployees().size() != 2) {
    errorMsg=errorMsg + "String_Node_Str" + manager_InCache.getManagedEmployees().size();
  }
  Employee manager_New=em.find(Employee.class,manager_New_id);
  if (!manager_New.getLastName().endsWith("String_Node_Str")) {
    errorMsg=errorMsg + "String_Node_Str";
  }
  it=manager_New.getManagedEmployees().iterator();
  while (it.hasNext()) {
    Employee emp=(Employee)it.next();
    if (emp.getId() == employee_2_NotInCache_id) {
      if (!emp.getLastName().endsWith("String_Node_Str")) {
        errorMsg=errorMsg + "String_Node_Str";
      }
    }
 else     if (emp.getId() == employee_2_InCache_id) {
      if (!emp.getLastName().endsWith("String_Node_Str")) {
        errorMsg=errorMsg + "String_Node_Str";
      }
    }
 else     if (emp.getId() == employee_3_New_id) {
      if (!emp.getLastName().endsWith("String_Node_Str")) {
        errorMsg=errorMsg + "String_Node_Str";
      }
    }
 else {
      errorMsg=errorMsg + "String_Node_Str" + emp.getLastName();
    }
  }
  if (manager_New.getManagedEmployees().size() != 3) {
    errorMsg=errorMsg + "String_Node_Str" + manager_InCache.getManagedEmployees().size();
  }
  Employee employee_4_New=em.find(Employee.class,employee_4_New_id);
  if (!employee_4_New.getLastName().endsWith("String_Node_Str")) {
    errorMsg=errorMsg + "String_Node_Str";
  }
  em.close();
  em=createEntityManager();
  try {
    em.getTransaction().begin();
    em.createQuery("String_Node_Str" + firstName + "String_Node_Str").executeUpdate();
    em.getTransaction().commit();
  }
 catch (  RuntimeException ex) {
    if (em.getTransaction().isActive()) {
      em.getTransaction().rollback();
    }
    if (errorMsg.length() == 0) {
      throw ex;
    }
  }
  clearCache();
  if (errorMsg.length() > 0) {
    fail(errorMsg);
  }
}

Fixed version (ground truth):
public void testClearWithFlush2(){
  String firstName="String_Node_Str";
  EntityManager em=createEntityManager();
  int employee_1_NotInCache_id=0;
  int employee_2_NotInCache_id=0;
  int manager_NotInCache_id=0;
  em.getTransaction().begin();
  try {
    Employee employee_1_NotInCache=new Employee();
    employee_1_NotInCache.setFirstName(firstName);
    employee_1_NotInCache.setLastName("String_Node_Str");
    Employee employee_2_NotInCache=new Employee();
    employee_2_NotInCache.setFirstName(firstName);
    employee_2_NotInCache.setLastName("String_Node_Str");
    Employee manager_NotInCache=new Employee();
    manager_NotInCache.setFirstName(firstName);
    manager_NotInCache.setLastName("String_Node_Str");
    manager_NotInCache.addManagedEmployee(employee_1_NotInCache);
    em.persist(manager_NotInCache);
    em.persist(employee_1_NotInCache);
    em.persist(employee_2_NotInCache);
    em.getTransaction().commit();
    employee_1_NotInCache_id=employee_1_NotInCache.getId();
    employee_2_NotInCache_id=employee_2_NotInCache.getId();
    manager_NotInCache_id=manager_NotInCache.getId();
  }
 catch (  RuntimeException ex) {
    if (em.getTransaction().isActive()) {
      em.getTransaction().rollback();
    }
    em.close();
    throw ex;
  }
  clearCache();
  em=createEntityManager();
  int employee_1_InCache_id=0;
  int employee_2_InCache_id=0;
  int manager_InCache_id=0;
  em.getTransaction().begin();
  try {
    Employee employee_1_InCache=new Employee();
    employee_1_InCache.setFirstName(firstName);
    employee_1_InCache.setLastName("String_Node_Str");
    Employee employee_2_InCache=new Employee();
    employee_2_InCache.setFirstName(firstName);
    employee_2_InCache.setLastName("String_Node_Str");
    Employee manager_InCache=new Employee();
    manager_InCache.setFirstName(firstName);
    manager_InCache.setLastName("String_Node_Str");
    manager_InCache.addManagedEmployee(employee_1_InCache);
    em.persist(manager_InCache);
    em.persist(employee_1_InCache);
    em.persist(employee_2_InCache);
    em.getTransaction().commit();
    employee_1_InCache_id=employee_1_InCache.getId();
    employee_2_InCache_id=employee_2_InCache.getId();
    manager_InCache_id=manager_InCache.getId();
  }
 catch (  RuntimeException ex) {
    if (em.getTransaction().isActive()) {
      em.getTransaction().rollback();
    }
    em.close();
    throw ex;
  }
  em=createEntityManager();
  int employee_1_New_id=0;
  int employee_2_New_id=0;
  int employee_3_New_id=0;
  int employee_4_New_id=0;
  int manager_New_id=0;
  em.getTransaction().begin();
  try {
    Employee employee_1_New=new Employee();
    employee_1_New.setFirstName(firstName);
    employee_1_New.setLastName("String_Node_Str");
    em.persist(employee_1_New);
    employee_1_New_id=employee_1_New.getId();
    Employee employee_2_New=new Employee();
    employee_2_New.setFirstName(firstName);
    employee_2_New.setLastName("String_Node_Str");
    em.persist(employee_2_New);
    employee_2_New_id=employee_2_New.getId();
    Employee employee_3_New=new Employee();
    employee_3_New.setFirstName(firstName);
    employee_3_New.setLastName("String_Node_Str");
    em.persist(employee_3_New);
    employee_3_New_id=employee_3_New.getId();
    Employee employee_4_New=new Employee();
    employee_4_New.setFirstName(firstName);
    employee_4_New.setLastName("String_Node_Str");
    em.persist(employee_4_New);
    employee_4_New_id=employee_4_New.getId();
    Employee manager_New=new Employee();
    manager_New.setFirstName(firstName);
    manager_New.setLastName("String_Node_Str");
    em.persist(manager_New);
    manager_New_id=manager_New.getId();
    Employee employee_1_NotInCache=em.find(Employee.class,employee_1_NotInCache_id);
    employee_1_NotInCache.setLastName(employee_1_NotInCache.getLastName() + "String_Node_Str");
    Employee employee_2_NotInCache=em.find(Employee.class,employee_2_NotInCache_id);
    employee_2_NotInCache.setLastName(employee_2_NotInCache.getLastName() + "String_Node_Str");
    Employee manager_NotInCache=em.find(Employee.class,manager_NotInCache_id);
    manager_NotInCache.setLastName(manager_NotInCache.getLastName() + "String_Node_Str");
    Employee employee_1_InCache=em.find(Employee.class,employee_1_InCache_id);
    employee_1_InCache.setLastName(employee_1_InCache.getLastName() + "String_Node_Str");
    Employee employee_2_InCache=em.find(Employee.class,employee_2_InCache_id);
    employee_2_InCache.setLastName(employee_2_InCache.getLastName() + "String_Node_Str");
    Employee manager_InCache=em.find(Employee.class,manager_InCache_id);
    manager_InCache.setLastName(manager_InCache.getLastName() + "String_Node_Str");
    manager_NotInCache.addManagedEmployee(employee_1_New);
    manager_InCache.addManagedEmployee(employee_2_New);
    manager_New.addManagedEmployee(employee_3_New);
    manager_New.addManagedEmployee(employee_2_NotInCache);
    manager_New.addManagedEmployee(employee_2_InCache);
    em.flush();
    em.clear();
    em.getTransaction().commit();
  }
 catch (  RuntimeException ex) {
    if (em.getTransaction().isActive()) {
      em.getTransaction().rollback();
    }
    em.close();
    throw ex;
  }
  String errorMsg="String_Node_Str";
  em=createEntityManager();
  Employee manager_NotInCache=em.find(Employee.class,manager_NotInCache_id);
  if (!manager_NotInCache.getLastName().endsWith("String_Node_Str")) {
    errorMsg=errorMsg + "String_Node_Str";
  }
  Iterator it=manager_NotInCache.getManagedEmployees().iterator();
  while (it.hasNext()) {
    Employee emp=(Employee)it.next();
    if (emp.getId() == employee_1_NotInCache_id) {
      if (!emp.getLastName().endsWith("String_Node_Str")) {
        errorMsg=errorMsg + "String_Node_Str";
      }
    }
 else     if (emp.getId() == employee_1_New_id) {
      if (!emp.getLastName().endsWith("String_Node_Str")) {
        errorMsg=errorMsg + "String_Node_Str";
      }
    }
 else {
      errorMsg=errorMsg + "String_Node_Str" + emp.getLastName();
    }
  }
  if (manager_NotInCache.getManagedEmployees().size() != 2) {
    errorMsg=errorMsg + "String_Node_Str" + manager_NotInCache.getManagedEmployees().size();
  }
  Employee manager_InCache=em.find(Employee.class,manager_InCache_id);
  if (!manager_InCache.getLastName().endsWith("String_Node_Str")) {
    errorMsg=errorMsg + "String_Node_Str";
  }
  it=manager_InCache.getManagedEmployees().iterator();
  while (it.hasNext()) {
    Employee emp=(Employee)it.next();
    if (emp.getId() == employee_1_InCache_id) {
      if (!emp.getLastName().endsWith("String_Node_Str")) {
        errorMsg=errorMsg + "String_Node_Str";
      }
    }
 else     if (emp.getId() == employee_2_New_id) {
      if (!emp.getLastName().endsWith("String_Node_Str")) {
        errorMsg=errorMsg + "String_Node_Str";
      }
    }
 else {
      errorMsg=errorMsg + "String_Node_Str" + emp.getLastName();
    }
  }
  if (manager_InCache.getManagedEmployees().size() != 2) {
    errorMsg=errorMsg + "String_Node_Str" + manager_InCache.getManagedEmployees().size();
  }
  Employee manager_New=em.find(Employee.class,manager_New_id);
  if (!manager_New.getLastName().endsWith("String_Node_Str")) {
    errorMsg=errorMsg + "String_Node_Str";
  }
  it=manager_New.getManagedEmployees().iterator();
  while (it.hasNext()) {
    Employee emp=(Employee)it.next();
    if (emp.getId() == employee_2_NotInCache_id) {
      if (!emp.getLastName().endsWith("String_Node_Str")) {
        errorMsg=errorMsg + "String_Node_Str";
      }
    }
 else     if (emp.getId() == employee_2_InCache_id) {
      if (!emp.getLastName().endsWith("String_Node_Str")) {
        errorMsg=errorMsg + "String_Node_Str";
      }
    }
 else     if (emp.getId() == employee_3_New_id) {
      if (!emp.getLastName().endsWith("String_Node_Str")) {
        errorMsg=errorMsg + "String_Node_Str";
      }
    }
 else {
      errorMsg=errorMsg + "String_Node_Str" + emp.getLastName();
    }
  }
  if (manager_New.getManagedEmployees().size() != 3) {
    errorMsg=errorMsg + "String_Node_Str" + manager_InCache.getManagedEmployees().size();
  }
  Employee employee_4_New=em.find(Employee.class,employee_4_New_id);
  if (!employee_4_New.getLastName().endsWith("String_Node_Str")) {
    errorMsg=errorMsg + "String_Node_Str";
  }
  em.close();
  em=createEntityManager();
  try {
    em.getTransaction().begin();
    List<Employee> list=em.createQuery("String_Node_Str" + firstName + "String_Node_Str").getResultList();
    Iterator<Employee> i=list.iterator();
    while (i.hasNext()) {
      Employee e=i.next();
      if (e.getManager() != null) {
        e.getManager().removeManagedEmployee(e);
        e.setManager(null);
      }
      em.remove(e);
    }
    em.getTransaction().commit();
  }
 catch (  RuntimeException ex) {
    if (em.getTransaction().isActive()) {
      em.getTransaction().rollback();
    }
    if (errorMsg.length() == 0) {
      throw ex;
    }
  }
  clearCache();
  if (errorMsg.length() > 0) {
    fail(errorMsg);
  }
}