title: JPA merge results in duplicate entry of foreign entity
body: <p>QUESTIONS:</p>
  
  <p>Does anyone know how to merge without having <code>EntityManager</code> trying to re-insert the foreign entity?</p>
  
  <p>SCENARIO:</p>
  
  <p>Just to set up a scenario that closely matches my case: I have two entities</p>
  
  <pre><code>@Entity
  @Table(name = "login", catalog = "friends", uniqueConstraints =
  @UniqueConstraint(columnNames = "username"))
  public class Login implements java.io.Serializable{
  
     private static final long serialVersionUID = 1L;
     @Id
     @GeneratedValue(strategy = IDENTITY)
     @Column(name = "id", unique = true, nullable = false)
     private Integer id;
     @Column(name = "username", unique = true, nullable = false, length = 50)
     private String username;
     @Column(name = "password", nullable = false, length = 250)
     private String password;
  }
  
  @Entity
  @Table(name = "friendshiptype", catalog = "friends")
  public class FriendshipType implements java.io.Serializable{
  
     private static final long serialVersionUID = 1L;
     @Id
     @GeneratedValue(strategy = IDENTITY)
     @Column(name = "id", unique = true, nullable = false)
     private Integer id;
     @OneToOne(fetch = FetchType.LAZY)
     @JoinColumn(name = "username")
     private Login login;
        @Column(name = "type", unique = true, length = 32)
     private String type;
     ...//other fields go here
  }
  </code></pre>
  
  <p>Both the <code>Login</code> entity and the <code>FriendshipType</code> entity are persisted to the database separately. Then, later, I need to merge a <code>Login</code> row with a <code>FriendshipType</code> row. When I call <code>entityManager.merge(friendship)</code>, it tries to insert a new <code>Login</code> which of course results in the following error</p>
  
  <pre><code>Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry 'myUserName1350319637687' for key 'username'
  Error Code: 1062
  Call: INSERT INTO friends.login (password, username) VALUES (?, ?)
  </code></pre>
  
  <p>My question, again, is how do I merge two objects without having enityManager trying to reinsert the foreign object?</p>