java - Spring Data 不保存相关实体
问题描述
我有几个实体
@Entity
@Table(name = "A")
public class A {
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "alIdSeq")
@SequenceGenerator(name = "aIdSeq", sequenceName = "a_id_seq", allocationSize = 1)
private Long id;
@JoinColumn(name = "a_id", referencedColumnName = "id")
@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.PERSIST)
private List<B> b = new ArrayList<>();
}
@Entity
@Table(name = "B")
public class B {
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "blIdSeq")
@SequenceGenerator(name = "aIdSeq", sequenceName = "b_id_seq", allocationSize = 1)
private Long id;
}
示例:当我从 repo 获取对象 A 并将新对象 B 添加到列表时,存储库不会保存新对象 B。
A a = aRepository.findById(someId);
B b = new B();
a.getB().add(B);
注意: sql表不包含new B。
解决方案
您的对象a
已经创建并且持久操作的主要问题没有发生。如果您尝试A
使用某些B
元素创建(持久)新实体(),则 newB
将包含在 sql 表中。
推荐阅读
- php - 在 wordpress 管理样式中单击添加新页面时会中断
- angular - How to trigger Required Field Validation when tabbing through DatePicker form control?
- android - 如何在已经创建的 react native 项目中添加 sdk 版本?
- java - Mapstruct:如何将自定义映射器与 @MappingTarget 一起使用
- angular - Ionic 4 Slider autoplay
- javascript - 什么是多文件上传示例的 js 验证:name="files[]"
- swift - 使用 ESTabBarController 添加标签栏
- python - for el in x ->TypeError: 'float' object is not iterable
- .net - Many system libraries in build folder when referencing .netstandard project
- wix - 当安装程序在卸载过程中运行时,如何防止 WIX 条件检测?