Program: 14961
Project: 14961
Buggy source:
/** 
 * <table> <tbody> <tr valign="top"> <th>Scenario</th> <th><tt>EntityManager.persist</tt></th> <th><tt>EntityManager.merge</tt></th> <th><tt>SessionManager.saveOrUpdate</tt></th> </tr> <tr valign="top"> <th>Object passed was never persisted</th> <td>1. Object added to persistence context as new entity<br> 2. New entity inserted into database at flush/commit</td> <td>1. State copied to new entity.<br> 2. New entity added to persistence context<br> 3. New entity inserted into database at flush/commit<br> 4. New entity returned</td> <td>1. Object added to persistence context as new entity<br> 2. New entity inserted into database at flush/commit</td> </tr> <tr valign="top"> <th>Object was previously persisted, but not loaded in this persistence context</th> <td>1. <tt>EntityExistsException</tt> thrown (or a <tt>PersistenceException</tt> at flush/commit)</td> <td>2. Existing entity loaded.<br> 2. State copied from object to loaded entity<br> 3. Loaded entity updated in database at flush/commit<br> 4. Loaded entity returned</td> <td>1. Object added to persistence context<br> 2. Loaded entity updated in database at flush/commit</td> </tr> <tr valign="top"> <th>Object was previously persisted and already loaded in this persistence context</th> <td>1. <tt>EntityExistsException</tt> thrown (or a <tt>PersistenceException</tt> at flush or commit time)</td> <td>1. State from object copied to loaded entity<br> 2. Loaded entity updated in database at flush/commit<br> 3. Loaded entity returned</td> <td>1. <tt>NonUniqueObjectException</tt> thrown</td> </tr> </tbody> </table>
 * @throws ConstraintViolationException
 * @param newObject
 */
public static <T>T merge(final T newObject) throws ConstraintViolationException {
  try {
    T persistedObject=getTransaction(newObject).getTxState().getEntityManager().merge(newObject);
    return persistedObject == newObject ? newObject : persistedObject;
  }
 catch (  final RuntimeException ex) {
    PersistenceExceptions.throwFiltered(ex);
    throw ex;
  }
}

Fixed version (ground truth):
/** 
 * <table> <tbody> <tr valign="top"> <th>Scenario</th> <th><tt>EntityManager.persist</tt></th> <th><tt>EntityManager.merge</tt></th> <th><tt>SessionManager.saveOrUpdate</tt></th> </tr> <tr valign="top"> <th>Object passed was never persisted</th> <td>1. Object added to persistence context as new entity<br> 2. New entity inserted into database at flush/commit</td> <td>1. State copied to new entity.<br> 2. New entity added to persistence context<br> 3. New entity inserted into database at flush/commit<br> 4. New entity returned</td> <td>1. Object added to persistence context as new entity<br> 2. New entity inserted into database at flush/commit</td> </tr> <tr valign="top"> <th>Object was previously persisted, but not loaded in this persistence context</th> <td>1. <tt>EntityExistsException</tt> thrown (or a <tt>PersistenceException</tt> at flush/commit)</td> <td>2. Existing entity loaded.<br> 2. State copied from object to loaded entity<br> 3. Loaded entity updated in database at flush/commit<br> 4. Loaded entity returned</td> <td>1. Object added to persistence context<br> 2. Loaded entity updated in database at flush/commit</td> </tr> <tr valign="top"> <th>Object was previously persisted and already loaded in this persistence context</th> <td>1. <tt>EntityExistsException</tt> thrown (or a <tt>PersistenceException</tt> at flush or commit time)</td> <td>1. State from object copied to loaded entity<br> 2. Loaded entity updated in database at flush/commit<br> 3. Loaded entity returned</td> <td>1. <tt>NonUniqueObjectException</tt> thrown</td> </tr> </tbody> </table>
 * @throws ConstraintViolationException
 * @param newObject
 * @throws NoSuchElementException 
 * @throws TransactionException 
 */
public static <T>T merge(final T newObject) throws ConstraintViolationException, TransactionException, NoSuchElementException {
  if (!isPersistent(newObject)) {
    return uniqueResult(newObject);
  }
 else {
    try {
      T persistedObject=getTransaction(newObject).getTxState().getEntityManager().merge(newObject);
      return persistedObject == newObject ? newObject : persistedObject;
    }
 catch (    final RuntimeException ex) {
      PersistenceExceptions.throwFiltered(ex);
      throw ex;
    }
  }
}