JPA - 使用mappedBy属性来定义拥有实体的区别 [英] JPA - difference in the use of the mappedBy property to define the owning entity

查看:815
本文介绍了JPA - 使用mappedBy属性来定义拥有实体的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下两个声明的区别是什么



B是自己的一方

  @Entity 
class A {
@Id int id;

@OneToOne
B b;
}

@实体
class B {
@Id int id;

@OneToOne(mappedBy =b)
A a;
}

A是自己的一方

  @Entity 
class A {
@Id int id;

@OneToOne(mappedBy =a)
B b;
}

@实体
class B {
@Id int id;

@OneToOne
A a;

$ / code>

在普通SQL中想到这一点,我认为它与两个表都有另一个表的外键。我不明白的是,指定哪个实体是拥有方的效果,即使用'mappedBy'属性。这实际上达到了什么,因为我不相信在正常的SQL中有等价物。 JPA 2.0规范第2.9节写道:

lockquote

关系可以是双向的或单向的。双向关系既有拥有方也有逆向(非拥有)方。单向关系只有一个拥有的一面。关系的拥有方确定数据库中关系的更新,如第3.2.4节所述。



以下规则适用于双向关系:




  • 双向关系的反面必须通过使用 mappedBy OneToOne OneToMany ManyToMany 注释的元素。 mappedBy 元素指定属于关系所有者的实体中的属性或字段。

  • 一对一-many / many-to-one双向关系必须是拥有方,因此 mappedBy 元素不能在 ManyToOne annotation。

  • 对于一对一的双向关系,拥有的一面对应于包含相应外键的一面。许多对多的双向关系可能都是自己的一方。


第3.2.4节是:


持久实体的状态在事务提交时与数据库同步。这种同步包括向数据库写入任何对持久化实体及其关系的更新,如上所述。


受管实体之间的双向关系将基于关系拥有方所持有的引用而持续存在。开发人员有责任保持内存中的引用保持在持有者一方,而保留在内存中的引用在更改时保持一致。
在单向一对一和一对多关系的情况下,开发者有责任确保关系的语义得到遵守。


确保对关系的反面进行更改会导致拥有者的适当更新,这一点尤为重要,以确保在更改时不会丢失同步到数据库。




What exactly is the difference in the following two declarations

B is the owning side

@Entity
class A {
   @Id int id;

   @OneToOne
   B b;
}

@Entity
class B {
   @Id int id;

   @OneToOne(mappedBy="b")
   A a;
}

A is the owning side

@Entity
class A {
   @Id int id;

   @OneToOne(mappedBy="a")
   B b;
}

@Entity
class B {
   @Id int id;

   @OneToOne
   A a;
}

Thinking of this in "normal SQL" i think it is the same as having two tables each having the other table's foreign key. What i don't understand though is what is the effect of specifying which entity is the owning side i.e using the 'mappedBy' property. What does this actually achieve as i don't believe there is an equivalent in normal SQL.

解决方案

The JPA 2.0 specification, section 2.9, writes:

Relationships may be bidirectional or unidirectional. A bidirectional relationship has both an owning side and an inverse (non-owning) side. A unidirectional relationship has only an owning side. The owning side of a relationship determines the updates to the relationship in the database, as described in section 3.2.4.

The following rules apply to bidirectional relationships:

  • The inverse side of a bidirectional relationship must refer to its owning side by use of the mappedBy element of the OneToOne, OneToMany, or ManyToMany annotation. The mappedBy element designates the property or field in the entity that is the owner of the relationship.
  • The many side of one-to-many / many-to-one bidirectional relationships must be the owning side, hence the mappedBy element cannot be specified on the ManyToOne annotation.
  • For one-to-one bidirectional relationships, the owning side corresponds to the side that contains the corresponding foreign key.
  • For many-to-many bidirectional relationships either side may be the owning side.

The relevant parts of section 3.2.4 are:

The state of persistent entities is synchronized to the database at transaction commit. This synchronization involving writing to the database any updates to persistent entities and their relationships as specified above.

and

Bidirectional relationships between managed entities will be persisted based on references held by the owning side of the relationship. It is the developer’s responsibility to keep the in-memory references held on the owning side and those held on the inverse side consistent with each other when they change. In the case of unidirectional one-to-one and one-to-many relationships, it is the developer’s responsibility to insure that the semantics of the relationships are adhered to.

It is particularly important to ensure that changes to the inverse side of a relationship result in appropriate updates on the owning side, so as to ensure the changes are not lost when they are synchronized to the database.

这篇关于JPA - 使用mappedBy属性来定义拥有实体的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆