title: JPA EntityManager: merge() is trying to create a new row in the db - why?
body: <p>I'm using JPA through the Play Framework.</p>
  
  <p>I'm checking to see if a User object is cached, and if so I retrieve it and merge() it such that I can update fields and save the changes later:</p>
  
  <pre><code>user = (User) Cache.get("user-auth-" + sessionAuthToken);
  if (user != null) {
      user = user.merge();  // I believe this is the same as EntityManager.merge()
  }
  </code></pre>
  
  <p>However when I do this I get the following error:</p>
  
  <pre><code>PersistenceException occured : 
    org.hibernate.exception.ConstraintViolationException: 
      could not insert: [models.User]
  ...
  Caused by: com.mysql.jdbc.exceptions.jdbc4.
     MySQLIntegrityConstraintViolationException: 
       Duplicate entry '1235411688335416533' for key 'authToken'
  </code></pre>
  
  <p>It seems like its trying to insert a new user, even though this user should be, and is already in the database.  Why would merge() do that?</p>
  
  <p>Or perhaps I'm going about this entirely the wrong way - advice would be appreciated.</p>