How to update the field of EmbeddedID in Spring JPA
I have a RecordId class, which is embeddable and referenced as EmbeddedId in the Record class. RecordId has version and date fields. During the update operation for Record, I have to update the field of RecordId, which is the version. I’m using the code below to do this.
existingRecord.getRecordId().setVersion(recordData.getRecordId().getVersion());
recordRepository.save(existingRecord);
But the data in the database is not updated.
Is there a limit to updating the EmbeddedId field.
// 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()) &&
Objects.equals(getEnvironmentType(),
that.getEnvironmentType());
}
@Override
public int hashCode() {
return Objects.hash(getRecordDomain(), getEnvironmentType());
}
}
Record Class with Embedded Id uses the embeddable class described above as the composite key.
@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;
}
}
JPA Reposiotory interface and Record class.
// JPA Repository
public interface RecordRepository extends JpaRepository<Record, RecordId>{
}
Solution
You cannot update the ID of an entity because this ID is used to create a primary key in the relational database. It will then be used to generate SQL update statements.
Suppose you have an existing database with only one record with ID = A.
Then change this id to B in java. JPA converts it to some SQL, for example
UPDATE record WHERE Id = 'B'
But there is no record of ‘B’ as the ID!
It turns out that there is no update in the database at all.
In this case, you should probably use the generated ID.