title: How to update fields of an EmbeddedID in Spring JPA
body: <p>I have a RecordId class which is Embeddable and is refereed in Record class as an EmbeddedId. RecordId has fields of version and date. During the update operation of the Record, I have to update field of RecordId i.e. version. I'm using the below code to do that.</p>
  
  <pre><code>existingRecord.getRecordId().setVersion(recordData.getRecordId().getVersion());
  recordRepository.save(existingRecord);
  </code></pre>
  
  <p>But data is not getting updated in the database. </p>
  
  <p>Is there a limitation on updating the fields of the EmbeddedId.</p>
  
  <pre><code>// Embeddable class
  
  @Embeddable
  public class RecordId implements Serializable{
  /**
   * 
   */
  private static final long serialVersionUID = 8167040004250354298L;
  
  @Column(name="Record_DOMAIN", nullable = false)
  @NotNull(message = "Record Domain cannot be empty")
  @JsonProperty("RecordDomain")
  private String recordDomain;
  
  @Column(name="ENVIRONMENT_TYPE", nullable = false)
  @NotNull(message = "Environment Type cannot be empty")
  @Enumerated(EnumType.STRING)
  @JsonProperty("environmentType")
  private EnvironmentTypes environmentType;
  
  @Column(name="UPGRADE", nullable = false)
  @JsonProperty("upgrade")
  private boolean upgrade;
  
  @Column(name="VERSION", nullable = false)
  @JsonProperty("version")
  private String version = "2.0";
  
  @ApiModelProperty(value = "Enironment type for which the Record Domain is 
  created.", required = true )
  public EnvironmentTypes getEnvironmentType() {
      return environmentType;
  }
  
  public void setEnvironmentType(EnvironmentTypes environmentType) {
      this.environmentType = environmentType;
  }
  
  @ApiModelProperty(value = "Cusotmer Domain name", required = true )
  public String getRecordDomain() {
      return recordDomain;
  }
  
  public void setRecordDomain(String recordDomain) {
      this.recordDomain = recordDomain;
  }
  
  
  public RecordId(){
  }
  
  public RecordId(String recordDomain, EnvironmentTypes environmentType){
      this.recordDomain = recordDomain;
      this.environmentType = environmentType;
  }
  
  public RecordId(String recordDomain, EnvironmentTypes environmentType, 
  boolean upgrade, String version){
      this.recordDomain = recordDomain;
      this.environmentType = environmentType;
      this.upgrade = upgrade;
      this.version = version;
  }
  
  public boolean isUpgrade() {
      return upgrade;
  }
  
  public void setUpgrade(boolean upgrade) {
      this.upgrade = upgrade;
  }
  
  public String getVersion() {
      return version;
  }
  
  public void setVersion(String version) {
      if( version != null) {
          this.version = version;
      }else {
          this.version = "18.2";
      }
  }
  
  @Override
  public boolean equals(Object o) {
      if (this == o) return true;
      if (!(o instanceof RecordId)) return false;
      RecordId that = (RecordId) o;
      return Objects.equals(getRecordDomain(), that.getRecordDomain()) &amp;&amp;
              Objects.equals(getEnvironmentType(), 
      that.getEnvironmentType());
  }
  
  @Override
  public int hashCode() {
      return Objects.hash(getRecordDomain(), getEnvironmentType());
  }
  
  }
  </code></pre>
  
  <p>Record Class with Embedded Id which uses the above embeddable class as composite key.</p>
  
  <pre><code>@Entity
  @Table(name="RECORD_DATA") 
  public class Record implements Serializable{
  
  private static final long serialVersionUID = 1L;
  
  @EmbeddedId
  private RecordId recordId;
  
  public RecordId getRecordId() {
      return recordId;
  }
  
  public void setRecordId(RecordId recordId) {
      this.recordId = recordId;
  }
  
  public Record() {
  }
  
  public Record(RecordId recordId) {
      this.recordId = recordId;
  }
  }
  </code></pre>
  
  <p>JPA Reposiotory interface with the Record class.</p>
  
  <pre><code>// JPA Repository
  public interface RecordRepository extends JpaRepository&lt;Record, RecordId&gt;{
  }
  </code></pre>