title: JPA Hibernate merge performs insert instead of update
body: <p>I have a simple test, where I am trying to update the object, but merge seems to be performing an insert instead of update. </p>
  
  <pre><code>@RunWith(SpringJUnit4ClassRunner.class)
  @ContextConfiguration(locations = {"classpath:spring/app-context.xml","classpath:spring/testdb-context.xml"})
  public class UserJPATest {
  @Test
  public void testUpdate() throws Exception {
      System.out.println("update");
  
      User entity = ObjectManager.USER_DAO.findById(3L);
      entity.setUsername("harryUpdate");
  
      ObjectManager.USER_DAO.update(entity);
  
      User selEntity = ObjectManager.USER_DAO.findById(3L);
      Assert.assertEquals(entity.getUsername(),selEntity.getUsername());
  }
  </code></pre>
  
  <p>}</p>
  
  <p>This is my update method</p>
  
  <pre><code>@Override
  @Transactional(propagation= Propagation.REQUIRES_NEW)
  public T update(T entity) throws Exception {
      try {
          T merged = entityManager.merge(entity);
          return merged;
      } catch (Exception e) {
          e.printStackTrace();
          throw new Exception(e);
      }
  }
  </code></pre>
  
  <p>Update to code </p>
  
  <pre><code>@Override
  @Transactional(propagation= Propagation.REQUIRES_NEW)
  public T update(T entity) throws Exception {
      try {
          T merged = null;
          BaseEntity baseEntity = null;
          if(entity instanceof BaseEntity){
              baseEntity = (BaseEntity)entity;
              merged = entityManager.find(entityClass, baseEntity.getId());
          }
          merged = entityManager.merge(entity);
          entityManager.flush();
          return merged;
      } catch (Exception e) {
          e.printStackTrace();
          throw new Exception(e);
      }
  }
  </code></pre>
  
  <p><br />
  Now I get the following error Could not commit JPA transaction; nested exception is javax.persistence.RollbackException: Transaction marked as rollbackOnly</p>