有没有办法在 parent 不存在的情况下保存 child JPA 实体?
Is there a way to save a child JPA entity without the parent being present?
我有两个实体以 parent child 方式相关,通过 spring 数据 JPA 中的 @OneToMany 双向关系。
基础表是通过外部提要填充的,有时我们会在 parent 之前收到 child。 primary/foreign 键是从传入的提要中使用的,因此我们提前知道它们是什么(尽管我们没有对数据库强制执行 fk 约束)。数据最终会一致。
有没有办法插入 child 实体并引用 parent 键,即使 parent 尚不存在。
我们正在使用最新版本的 Spring 数据 jpa,以休眠作为我们的提供者。
Is there a way to insert a child entity with a reference to the parent key, even though the parent does not yet exist.
好吧,一种方法是将 native SQL query 添加到您的 ChildEntityRepository
,例如 (.persistWhenParentNotAvailable()
).
当您知道 parent 数据在给定点不可用时,您使用此方法保存数据。
我想你也可以尝试手动给 child 一个 parent object,只设置 ID 字段,并确保 NOT
配置了级联对于这个 child->parent
关系。
我有两个实体以 parent child 方式相关,通过 spring 数据 JPA 中的 @OneToMany 双向关系。
基础表是通过外部提要填充的,有时我们会在 parent 之前收到 child。 primary/foreign 键是从传入的提要中使用的,因此我们提前知道它们是什么(尽管我们没有对数据库强制执行 fk 约束)。数据最终会一致。
有没有办法插入 child 实体并引用 parent 键,即使 parent 尚不存在。
我们正在使用最新版本的 Spring 数据 jpa,以休眠作为我们的提供者。
Is there a way to insert a child entity with a reference to the parent key, even though the parent does not yet exist.
好吧,一种方法是将 native SQL query 添加到您的 ChildEntityRepository
,例如 (.persistWhenParentNotAvailable()
).
当您知道 parent 数据在给定点不可用时,您使用此方法保存数据。
我想你也可以尝试手动给 child 一个 parent object,只设置 ID 字段,并确保 NOT
配置了级联对于这个 child->parent
关系。