title: How to update Child entity along with Parent entity in Spring Boot?
body: <p>I am trying to update the parent entity and child entity (comes with parent) using JPA. I tried many ways but I couldn't do it. It gives me the following error.</p>
  <blockquote>
  <p>javax.persistence.EntityExistsException: A different object with the
  same identifier value was already associated with the session Contact(6)</p>
  </blockquote>
  <p>Following is the entity,</p>
  <blockquote>
  <p>Parent -&gt; User.java</p>
  </blockquote>
  <pre><code>@Entity
  public class User extends Serializable {
  
      private static final long serialVersionUID = 1L;
  
      @Id
      @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = &quot;seq_generator&quot;)
      @SequenceGenerator(name = &quot;seq_generator&quot;, sequenceName = &quot;seq_users&quot;, initialValue = 1, allocationSize = 1)
      private Long id;
      
      private String firstName;
      private String lastName;
      private String email;
      private String password;
      private String street;
      private String city;
      
      @OneToOne(cascade = CascadeType.ALL, mappedBy = &quot;userContact&quot;)
      private Contact contact;
  }
  </code></pre>
  <blockquote>
  <p>Child -&gt; Contact.java</p>
  </blockquote>
  <pre><code>@Entity
  public class Contact extends Serializable {
  
      private static final long serialVersionUID = 1L;
      
      @Getter(value = AccessLevel.NONE)
      @Setter(value = AccessLevel.NONE)
      @Id
      @Column(insertable = false, updatable = false)
      private Long id;
  
      @MapsId
      @OneToOne
      @JoinColumn(name = &quot;id&quot;, nullable = false)
      private User userContact;
  
      private String phone;
  }
  </code></pre>
  <p>In my Service.java I am updating like this,</p>
  <pre><code>@Override
  @Transactional
  public User update(Long id, User user) {
      Optional&lt;User&gt; curUser = userRepo.findById(id);
      
      user.setId(curUser.get().getId());
      user.setStreet(curUser.get().getStreet());
      user.setCity(curUser.get().getCity());
  
      Contact contact = user.getContact();
      contact.setUserContact(user);
  
      user.setContact(contact);
  
      return userRepo.save(user);
  }
  </code></pre>
  <p>And the update JSON payload is like this,</p>
  <pre><code>{
      &quot;firstName&quot;: &quot;FirstName&quot;,
      &quot;lastName&quot;: &quot;LastName&quot;,
      &quot;email&quot;: &quot;Email&quot;,
      &quot;password&quot;: &quot;Password&quot;,
      &quot;contact&quot;: {
          &quot;phone&quot;: &quot;0123456789&quot;
      }
  }
  </code></pre>
  <p>So why I can't update this at once? The insert is working fine but this update is not working. What I am doing wrong here? I really appreciate it if anybody can help me. Thanks in advance.</p>
  <p>Here is the new way I tried,</p>
  <pre><code>@Override
  @Transactional
  public User update(Long id, User user) {
      Optional&lt;User&gt; curUser = userRepo.findById(id);
      
      curUser.setFirstName(user.getFirstName());
      curUser.setlastName(user.getLastName());
      curUser.setEmail(user.getEmail());
      curUser.setPassword(user.getPassword());
  
      Contact contact = user.getContact();
      contact.setUserContact(curUser);
  
      curUser.setContact(contact);
  
      return userRepo.save(curUser);
  }
  </code></pre>