How to update fields of an EmbeddedID in Spring JPA

Viewed 4034

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.

existingRecord.getRecordId().setVersion(recordData.getRecordId().getVersion());
recordRepository.save(existingRecord);

But data is not getting updated in the database.

Is there a limitation on updating the fields of the EmbeddedId.

// 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 which uses the above embeddable class as 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 with the Record class.

// JPA Repository
public interface RecordRepository extends JpaRepository<Record, RecordId>{
}
2 Answers

You can't update the ID of your entity, because this ID is used to create a primary key in the relational database. And will then be used to generate the sql update statement.

Suppose you have an existing database with only one record with ID = A. Then in java you change this id to B. JPA will convert this into some SQL like

UPDATE record WHERE Id = 'B'

But there is no record with 'B' as an ID ! It turns that no update at all is made in the database.

You should probably use a generated id in this case.

Can you change the equals method in RecordId.class ;

you can write easily like ;

@Override
public boolean equals(Object obj) {
    return super.equals(obj);
}
Related