hibernate一对多映射双向合并

时间:2018-06-12 12:56:59

标签: hibernate

我有两张桌子如下。父母与子女的关系。

父表:

parent_id是自动生成的主键

子表:

parent_idchild_id主键和parent_id是外键

当我尝试在主表中插入新行时,子表也插入(工作正常)。当我尝试使用合并操作更新父表时,子表中的无法插入新行但能够更新现有行并获得以下错误。

  

无法将值NULL插入列parent_id,表中   Test.Sample.child;列不允许空值。

INSERT失败。

1 个答案:

答案 0 :(得分:0)

尝试使用

 cascade=ALL 

根据您的引用映射进行注释。

点击此处了解更多详情。 What is the difference between persist() and merge() in Hibernate?

注意:如果您提供映射和样本使用方案,则可以更清楚地回答您的问题。