【发布时间】:2021-11-11 18:53:24
【问题描述】:
我能够通过适用于 GET 请求的复合 pk 获得这种多对一关系,但在保存新条目时遇到问题。
我有一个带有嵌入 ID 的父实体。
@Embeddable
public class AnnualServiceHistoryPK implements Serializable {
@Column(name = "year", columnDefinition = "int(4)")
Integer year;
@Column(name = "month", columnDefinition = "char(3)")
String month;
@Column(name = "route", columnDefinition = "varchar(32)")
String route;
这是放在父实体中的:
@Entity(name = "AnnualServiceHistory")
@Table(name = "annual_service_history")
public class AnnualServiceHistory extends Auditable<String> implements Serializable
{
@EmbeddedId
AnnualServiceHistoryPK annualServiceHistoryPK;
... other variables
@OneToMany(mappedBy = "annualServiceHistory", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
List<AnnualServiceHistoryNonMachine> annualServiceHistoryNonMachineList;
我有一个子实体,它从父实体映射嵌入的 id 并添加和额外的本地 PK。
public class AnnualServiceHistoryNonMachine extends Auditable<String> implements Serializable {
@Id
@MapsId
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "year", referencedColumnName = "year")
@JoinColumn(name = "month", referencedColumnName = "month")
@JoinColumn(name = "route", referencedColumnName = "route")
private AnnualServiceHistory annualServiceHistory;
@Id
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "toy_inventory", referencedColumnName = "id")
ToyInventory toyInventory;
... other variables
我的问题是,当我在级联过程中出于某种原因将父级与子级列表一起保存时,当它生成子密钥时,它只是从 MapsId 部分创建密钥并抱怨 toyInventory 密钥为空.我不确定为什么它会丢失这个 ID,因为在执行存储库保存之前,子类在对象中有 toyInventory 键。
这是我收到的错误: "java.sql.SQLIntegrityConstraintViolationException: 列 'toy_inventory' 不能为空"
【问题讨论】: