简体   繁体   English

双向 OneToMany 与 OneToOne 关联与 mappedBy 属性

[英]Bidirectional OneToMany vs OneToOne association with mappedBy attribute

I defined entities with @OneToOne bidirectional relationship.我定义了具有@OneToOne双向关系的实体。

Owner site of relationship ( Child class):所有者关系站点(类):

@OneToOne
@JoinColumn(name = "owner_id")
private Parent parent;

Other site ( Parent class):其他站点(类):

@OneToOne(mappedBy = "parent")
private Child child;

When I try to persist Parent who has child set (which is not persisted) then I got exception当我尝试坚持拥有子集的父母(未坚持)时,我遇到了异常

  • TransientPropertyValueException: object references an unsaved transient instance - save the transient instance before flushing TransientPropertyValueException:object 引用未保存的瞬态实例 - 在刷新之前保存瞬态实例

which looks understandable.这看起来可以理解。

But when I defined entities with bidirectional OneToMany/ManyToOne association like:但是,当我定义具有双向 OneToMany/ManyToOne 关联的实体时,例如:

The owning site of this relationship:此关系的拥有站点:

@ManyToOne
@JoinColumn(name = "owner_id")
private Parent parent;

The other side of the relationship:关系的另一端:

@OneToMany(mappedBy = "parent")
private Set<Child> childrens = new HashSet<>();

and try to persist Parent who has children collection set (which are not persisted) then I can do it and Hibernate persist only Parent.并尝试坚持拥有子集合集(未坚持)的父母,然后我可以做到,而 Hibernate 只坚持父母。

For me it looks strange because this behavior is not deterministic - I know that this example is not correctly and I can use CascadeType.ALL or persist it correctly.对我来说,这看起来很奇怪,因为这种行为不是确定性的——我知道这个例子不正确,我可以使用CascadeType.ALL或正确地坚持它。 Do you know why it looks that?你知道为什么它看起来像吗? Is that Hibernate use different algorithms to persist entities in @OneToOne/@OneToMany relationship Hibernate 是否使用不同的算法来保持@OneToOne/@OneToMany 关系中的实体

If you use cascade = PERSIST on the @OneToMany you should see the same error.如果您在 @OneToMany 上使用cascade = PERSIST @OneToMany您应该会看到相同的错误。 The difference is just that Hibernate does not try to "manage" *-to-many associations on the inverse side ie where the mappedBy is present.不同之处在于 Hibernate 不会尝试“管理”反面的 * 对多关联,即存在mappedBy的地方。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 OneToOne双向双向关联 - OneToOne Double Bidirectional Association OneToMany双向关联示例 - OneToMany bidirectional association example Intellij IDEA @OneToMany mappingBy属性错误解决 - Intellij IDEA @OneToMany mappedBy attribute error resolving JPA @OneToMany - Set vs List - 使用 Set 时无法从双向关联中删除一个子实体 - JPA @OneToMany - Set vs List - Not able to delete one child entity from bidirectional association when using Set 如何在休眠中正确设置双向关联@OneToMany - How to properly set bidirectional association @OneToMany in hibernate 具有@OneToMany双向关联的Hibernate映射映射 - Hibernate mapping map with @OneToMany bidirectional association 双向 JPA OneToMany/ManyToOne 关联中的“关联的反面”是什么? - What is “the inverse side of the association” in a bidirectional JPA OneToMany/ManyToOne association? 如果我们未在OneToMany批注中指定mapBy属性怎么办? - what if we do not specify mappedBy attribute in OneToMany annotation? 为什么hibernate执行两个查询以急切加载@OneToOne双向关联? - Why hibernate perform two queries for eager load a @OneToOne bidirectional association? Hibernate 4.2中具有@OneToMany的性能-单向与双向 - Perfomance in Hibernate 4.2 with @OneToMany - Unidirectional vs Bidirectional
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM