gpt4 book ai didi

java - 如何在 Spring JPA 中更新 EmbeddedID 的字段

转载 作者:行者123 更新时间:2023-11-30 02:03:03 24 4
gpt4 key购买 nike

我有一个可嵌入的 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;
}
}

JPA Repository 与 Record 类的接口(interface)。

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

最佳答案

您无法更新实体的 ID,因为该 ID 用于在关系数据库中创建主键。然后将用于生成sql更新语句。

假设您的现有数据库只有一条 ID = A 的记录。然后在java中你将这个id更改为B。JPA会将其转换为一些SQL,例如

UPDATE record WHERE Id = 'B'

但是没有以'B'作为ID的记录!事实证明数据库中根本没有进行任何更新。

在这种情况下,您可能应该使用生成的 ID。

关于java - 如何在 Spring JPA 中更新 EmbeddedID 的字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52183949/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com