孩子的身份证不是从父母继承分配的 [英] Child's id is not assigned from parent on persist
问题描述
我正在浏览身份测序策略,并在以下语句
未持续存在父级分配的孩子ID。
常见问题是生成的Id是通过OneToOne或ManyToOne映射的子对象Id的一部分。在这种情况下,
因为JPA要求孩子为Id定义一个重复的基本映射
,所以它的Id将被插入为空。对此的一个解决方案是
将子标记作为insertable = false
,
updateable = false
,并使用普通的
JoinColumn定义OneToOne或ManyToOne,这将确保外键字段由
OneToOne或ManyToOne填充,而不是Basic。另一个选择是首先
坚持父母,然后在坚持孩子之前调用flush()。
有人可以解释这个作者试图传达给我们的问题是什么?
另外我看到 insertable = false,updateable = false
似乎我们正在将该列设置为只读,是属性的含义是什么?为什么我们要创建一个只读的列(没有插入和更新),请给我一个例子吗? 解决方案
基本上,作者试图说的是,如果是父母子女关系,我们必须先坚持孩子,然后将孩子与父母关联,然后坚持父母或让父母坚持孩子。这可以通过 insertable = false,updateable = false
。
insertable = false,updateable = false
意思是说,创建/更新相关实体的责任不在子实体中。你有一个孩子和一个家长。您希望在Parent实体的Child实体的@OneToMany关系中添加insertable = false,updatable = false,仅仅因为它不是子实体创建或更新父实体的责任。这是相反的。
I am going through the Identity Sequencing strategy and came across below statement
Child's id is not assigned from parent on persist.
A common issue is that the generated Id is part of a child object's Id through a OneToOne or ManyToOne mapping. In this case, because JPA requires that the child define a duplicate Basic mapping for the Id, its Id will be inserted as null. One solution to this is to mark the Column on the Id mapping in the child as
insertable=false
,updateable=false
, and define the OneToOne or ManyToOne using a normal JoinColumn this will ensure the foreign key field is populated by the OneToOne or ManyToOne not the Basic. Another option is to first persist the parent, then call flush() before persisting the child.
Can someone please explain what the issue the author is trying to convey to us?
Also I see insertable=false, updateable=false
seems to be like we are making the column as read-only, is that what the attributes mean? Why do we create a column which is just read-only (without insert & update), can you please give me an example?
Basically what the author is trying to say is that, in case of parent child relationship, we must persist the child first, then associate the child to the parent and then persist the parent OR let the parent persist the child. This can be done by insertable=false, updateable=false
.
What insertable=false, updateable=false
means is that, the responsibility of creating/updating the related entity in question isn't in the child entity. You have a Child and an Parent. You'd like to add insertable=false, updatable=false to the @OneToMany relationship with the Child entity in the Parent entity, simply because it's not the responsibility of the child entity to create or update a Parent. It's the other way round.
这篇关于孩子的身份证不是从父母继承分配的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!