title: detached entity passed to persist while using spring jpa with @mapsId
body: <p>I have two entities User and UserPersonalDetails with OneToOne mapping. Below are my classes</p>
  
  <p><strong><em>User class</em></strong></p>
  
  <pre><code>@Entity
  @Table
  public class User implements UserDetails {
  
      @Id
      @GeneratedValue(strategy = GenerationType.AUTO)
      Long userId;
  
      @Column(nullable = false)
      String firstName;
  
      @Column(nullable = false)
      String lastName;
  
  
      @Column(nullable = false,unique=true)
      String email;
  
      @Column
      String mobile;
  
      @Column(nullable = true)
      String password;
  
      @Column(nullable = false)
      String role;
  
      @Column(nullable = false)
      boolean isAccountLocked;
  
      @Column(nullable = false)
      boolean isAccountDisabled;
  
      @Column(nullable = false)
      boolean isPasswordExipred;
  
      @Column(nullable = false)
      boolean isAccountExipred;
  
      public User() {}
  
  public User(long userId, String email, String role) {
      this.userId = userId;
      this.email = email;
      this.role = role;
  }
  </code></pre>
  
  <p><strong><em>UserPersonalDetails class</em></strong></p>
  
  <pre><code>@Entity
  @Table
  public class UserPersonalDetails {
  
      @Id
      Long userId;
  
      @JoinColumn(name = "userId")
      @OneToOne( cascade = CascadeType.MERGE)
      @MapsId
  
      User user;
  
      Long univercityId;
      Long collageId;
      String address;
      String parentContact;
      String tempAddress;
  </code></pre>
  
  <p>When I tried to saving the UserPersonalDetails object with spring jpa's <em>JpaRepository</em> it end up with below error </p>
  
  <blockquote>
    <p>nested exception is
    org.springframework.dao.InvalidDataAccessApiUsageException: detached
    entity passed to persist: com.asset.app.user.User; nested exception is
    org.hibernate.PersistentObjectException: detached entity passed to
    persist: com.asset.app.user.User]</p>
  </blockquote>
  
  <p>Note : I don't want to save/update the User object again while saving/updating UserPersonalDetails object in DB</p>