jpa - 内部带有 OneToOne 的 ElementCollection 不会级联“删除”
问题描述
我的模型设置如下:
@Table(name = "parent")
@Entity
public class Parent extends IdEntity {
@ElementCollection(fetch = EAGER)
@CollectionTable(
name = "foo",
joinColumns = @JoinColumn(name = "parent_id")
)
private List<Foo> foos = new ArrayList<>();
}
@Embeddable
public class Foo {
@NotNull
@OneToOne(optional = false, cascade = ALL)
private Bar bar;
}
@Entity
public class Bar extends IdEntity {
private String baz; // marked as unique in the DB
}
在Parent
实例的每次更新时,我都会收到一个异常,说有一个重复的Bar→baz
条目。
org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; constraint [bar.baz]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement
我手动清理foos
集合并在每次更新时重新输入条目addAll
。Foo
我的理解是,由于 的性质ElementCollection
,它级联所有操作并删除孤儿。由于我有一个cascade = ALL
on Foo→bar
then 这应该意味着所有这些条目也将被删除和读取。那么可能导致“重复输入”错误的原因是什么?
解决方案
推荐阅读
- json - openweathermap api json 凌空响应返回空
- flutter - Flutter:无法从 API 加载特定数据
- python - Python:将参数传递给类中的方法
- php - 计算表中的行数并更改单列的宽度..?
- amazon-web-services - 适用于 VOD 直播的 AWS MediaLive
- rest - Fuse Karaf rest-dsl-simple 丢失
- python - 无法通过 WSL 使用 conda 显示 matplotlib 的输出
- c - 使用 CMake 编译项目
- bash - 如何从 bash 拦截命令标准输入和标准输出?
- javascript - 获取隐藏按钮元素的选择器