我有一个可嵌入的RecordId类,在Record类中被称为EmbeddedId。 RecordId具有版本和日期字段。在记录的更新操作期间,我必须更新RecordId的字段,即版本。我正在使用下面的代码来做到这一点。
existingRecord.getRecordId().setVersion(recordData.getRecordId().getVersion());
recordRepository.save(existingRecord);
但是数据库中的数据没有更新。
在更新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());
}
}
使用上述可嵌入类作为组合键的带有嵌入式ID的记录类。
@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;
}
}
带有Record类的JPA仓库接口。
// JPA Repository
public interface RecordRepository extends JpaRepository<Record, RecordId>{
}
答案 0 :(得分:1)
您无法更新实体的ID,因为此ID用于在关系数据库中创建主键。然后将用于生成sql更新语句。
假设您有一个现有数据库,其中只有一条ID = A的记录。 然后在Java中将此ID更改为B。JPA会将其转换为类似SQL的
UPDATE record WHERE Id = 'B'
但是没有记录以“ B”作为ID! 事实证明,数据库中根本没有进行任何更新。
在这种情况下,您可能应该使用生成的ID。
答案 1 :(得分:0)
您可以在 RecordId.class 中更改equals方法吗?
您可以轻松地像;
@Override
public boolean equals(Object obj) {
return super.equals(obj);
}