我有类似的东西:
@Entity
@Table(name = "claim", schema = "test")
public class Claim implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "idClaim", unique = true, nullable = false)
private Integer idClaim;
@OneToOne(mappedBy = "claim", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JsonManagedReference
private ClaimReturnInfo claimReturnInfo;
@Column(name = "notes")
private String notes;
// Getters and setters
}
@Entity
@Table(name = "claim_returninfo", schema = "test")
public class ClaimReturnInfo implements Serializable {
@Id
@Column(name = "Claim_idClaim")
private Integer id;
@MapsId("Claim_idClaim")
@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "Claim_idClaim")
@JsonBackReference
private Claim claim;
@Column(name = "description")
private String description;
// Getters and setters
}
未自动生成ClaimReturnInfo Id,因为我们要从其父级(Claim)传播该Id。我们无法自动执行此操作,并且会出现此错误:当在ClaimReturnInfo中执行“ cascade”时,在调用save()之前必须手动分配此类的ID。
是否可以将Claim Id映射到ClaimReturnInfo Id还是应该手动执行?
即使我们在ClaimReturnInfo上手动设置了此ID,并且可以执行更新,但在尝试创建新的Claim时仍然会收到此错误:
// POST -> claimRepository.save() -> Error
{
"notes": "Some test notes on a new claim",
"claimReturnInfo": {
"description": "Test description for a new claimReturnInfo"
}
}
在服务实现中:
@Override
@Transactional
public Claim save(Claim claim) throws Exception {
if(null != claim.getClaimReturnInfo()) {
claim.getClaimReturnInfo().setId(claim.getIdClaim());
}
Claim claimSaved = claimRepository.save(claim);
return claimSaved;
}
最佳答案
我尝试使用以下映射,从您的评论中可以明显看出,Json对象已正确填充。
我注意到@MapsId注释是罪魁祸首,如果您查看@MapsId注释的文档,它会说
块引用
复合键中的属性名称
*关系属性对应的*。如果不
*提供,该关系映射实体的主要对象
*键
块引用
如果将@MapsId(“ Claim_idClaim”)更改为@MapsId,它将开始保留您的实体。
import javax.persistence.*;
@Entity
@Table(name = "CLAIM")
public class Claim {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "idClaim", unique = true, nullable = false)
private Long idClaim;
@Column(name = "notes")
private String notes;
@OneToOne(mappedBy = "claim", cascade = CascadeType.ALL, optional = false)
private ClaimReturnInfo claimReturnInfo;
public Long getIdClaim() {
return idClaim;
}
public String getNotes() {
return notes;
}
public void setNotes(String notes) {
this.notes = notes;
}
public ClaimReturnInfo getClaimReturnInfo() {
return claimReturnInfo;
}
public void setClaimReturnInfo(ClaimReturnInfo claimReturnInfo) {
if (claimReturnInfo == null) {
if (this.claimReturnInfo != null) {
this.claimReturnInfo.setClaim(null);
}
} else {
claimReturnInfo.setClaim(this);
}
this.claimReturnInfo = claimReturnInfo;
}
}
package com.hiber.hiberelations;
import javax.persistence.*;
@Entity
@Table(name = "CLAIM_RETURN_INFO")
public class ClaimReturnInfo {
@Id
@Column(name = "Claim_idClaim")
private Long childId;
@Column(name = "DESCRIPTION")
private String description;
@MapsId
@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "Claim_idClaim")
private Claim claim;
public Long getChildId() {
return childId;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Claim getClaim() {
return this.claim;
}
public void setClaim(Claim claim) {
this.claim = claim;
}
}