title: Merge an entity, change its id, merge again, cause "mapped to a primary key column in the database. Updates are not allowed" error
body: <p>I have a JPA program where EclipseLink is the Persistence provider. When I merge an user entity, change its ID, and try to merge the same user instance again, an error is thrown. I rewrite my code to illustrate my problem in the simplest way. </p>
  
  <pre><code>User user = userManager.find(1);
  userManager.merge(user);
  System.out.println("User is managed? "+userManager.contains(user);
  user.setId(2);
  userManager.merge(user);
  </code></pre>
  
  <p>The above code is not in a transaction context. userManager is a stateless session bean with an EntityManager injected.  When executed, the console prints:</p>
  
  <pre><code>User is managed? false
  
  Exception [EclipseLink-7251] (Eclipse Persistence Services - 2.1.3.v20110304-r9073): org.eclipse.persistence.exceptions.ValidationException
  Exception Description: The attribute [id] of class [demo.model.User] is mapped to a primary key column in the database. Updates are not allowed.
  </code></pre>
  
  <p>The exception occurs at the second merge() invocation.</p>
  
  <p>If I create a new user, sets its ID and merge it, it works:</p>
  
  <pre><code>User user = userManager.find(1);
  userManager.merge(user);
  System.out.println("User is managed? "+userManager.contains(user);
  User newUser = new User();
  newUser.setId(2);
  userManager.merge(newUser);
  </code></pre>
  
  <p>So what is the difference between the first scenario and second one? According to the JPA specification, as long as the entity is in detached state, the merge should succeed, right?
  (Assuming the entity with ID=2 exists)</p>
  
  <p>Why the EclipseLink provider seems to be bothered by the fact that the user entity has been merged before?</p>
  
  <p>Update: It seems to be an bug of EclipseLink. I have replaced the persistence provider from EclipseLink to Hibernate: </p>
  
  <p>I change </p>
  
  <pre><code>&lt;provider&gt;org.eclipse.persistence.jpa.PersistenceProvider&lt;/provider&gt;
  </code></pre>
  
  <p>to </p>
  
  <pre><code>&lt;provider&gt;org.hibernate.ejb.HibernatePersistence&lt;/provider&gt;    
  </code></pre>
  
  <p>No error has been thrown. </p>