【发布时间】:2022-09-27 17:16:48
【问题描述】:
使用休眠,插入到子级失败,并在子级上出现“参照完整性约束违规”。每个孩子的父 ID 都会递增。
// Parent: Composite primary key, one auto generated
@IdClass(PlanId.class)
public class PlanEntity {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = \"PlanIdGenerator\")
@SequenceGenerator(name = \"PlanIdGenerator\", sequenceName = \"PLAN_ID_SEQUENCE\", allocationSize = 1)
private Long id;
@Id
private Long version;
@OneToMany(cascade = CascadeType.ALL, mappedBy = \"planEntity\", fetch = FetchType.LAZY, orphanRemoval = true) //
private Collection<PlanGoalBucketEntity> goalBuckets = new ArrayList<>();
public void addPlanGoalBucketEntity(PlanGoalBucketEntity goalBucket) {
goalBuckets.add(goalBucket);
goalBucket.setPlanEntity(this);
}
public void removePosition(PlanGoalBucketEntity goalBucket) {
goalBuckets.remove(goalBucket);
goalBucket.setPlanEntity(null);
}
.....
}
//Child
public class PlanGoalBucketEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@ManyToOne(optional = false)
@JoinColumns({ @JoinColumn(name = \"plan_id\", referencedColumnName = \"id\"),
@JoinColumn(name = \"version\", referencedColumnName = \"version\") })
private PlanEntity planEntity;
.....
}
用一个 PlanGoalBucketEntity(子)插入到平面(父)就可以了。
插入具有多个子项的父项,它会因外键违规“参照完整性约束违规”而失败。插入第一个孩子很好,但第二个孩子失败了,因为它增加了第二个孩子的父 ID。
无法弄清楚出了什么问题。
标签: java spring-boot hibernate spring-data-jpa