HibernateException- identifier of an instance of Domain was altered from X to Y
<p>I am getting an exception when I am updating Parent record in spring data jpa. </p>

<p>This is my code:</p>

<p><strong>ParentEntity</strong></p>

<pre><code>@Entity
@Table(name = "CAMP")
@Getter
@Setter
public class Parent extends AbstractEntity  {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "tkeygenerator")
@GenericGenerator(name = "tkeygenerator", strategy = "com.custom.TKeyGenerator",
parameters = {@org.hibernate.annotations.Parameter(name = "sequence", value = "TKEY_SEQ")})
@Column(name = "TKEY", nullable = false)    
private String id;
@ManyToOne
@JoinColumn(name = "SUB_CAT_TYPE_CODE", referencedColumnName =   "SUB_CAT_TYPE_CODE")
private Child child;
@Column(name = "DATE")
@Basic
private LocalDate date;
}  
</code></pre>

<p><strong>Child Entity</strong></p>

<pre><code>@Entity
@Table(name = "SUB_CAT_TYPE")
@AttributeOverrides({
@AttributeOverride(name = "code", 
                   column = @Column(name = "SUB_CAT_TYPE_CODE", length = 30)),
@AttributeOverride(name = "description", 
                   column = @Column(name = "SUB_CAT_TYPE_DESC", length = 255))})
@EqualsAndHashCode(callSuper = true)
public class Child extends AbstractTypeDesc {}
</code></pre>

<p><strong>TestCode</strong> </p>

<pre><code>public Parent update(@PathVariable("id") String id, @Valid @RequestBody  UpdateDto dto) {

      Parent parentObj = parentRepository.findById(id);
      mapper.map(dto, parentObj); // Dozer to map incoming dto to domain                  

      childRepository.findByCode(dto.child().getCode())
                     .map(child -&gt; { 
                           parentObj.setChild(child);
                           return child;
                      });

      return parentRepository.save(parentObj); //Exception occurs here
}    
</code></pre>

<p>I am getting an exception while trying to update code variable of child entity in parent entity as fk. It says <code>can't alter code from X to Y</code>.</p>

<p>Any suggestion?</p>