[英]Non-owning entity side of @OneToOne/@ManyToOne/@ManyToMany
I am trying to understand the javax.persistence annotations @OneToOne , @ManyToOne and @ManyToMany .我试图理解 javax.persistence 注释@OneToOne 、 @ManyToOne和@ManyToMany 。 The descriptions for these annotations make a mention of
non-owning side
.这些注释的描述提到了
non-owning side
。 Specifically:具体来说:
[@OneToOne]: If the relationship is bidirectional, the non-owning side must use the mappedBy element of the OneToOne annotation to specify the relationship field or property of the owning side.
[@OneToOne]:如果关系是双向的,非拥有方必须使用OneToOne注解的mappedBy元素来指定拥有方的关系字段或属性。
[@ManyToOne]: If the relationship is bidirectional, the non-owning OneToMany entity side must used the mappedBy element to specify the relationship field or property of the entity that is the owner of the relationship.
[@ManyToOne]:如果关系是双向的,非拥有的OneToMany实体端必须使用mappedBy元素来指定关系的拥有者实体的关系字段或属性。
[@ManyToMany]: If the relationship is bidirectional, the non-owning side must use the mappedBy element of the ManyToMany annotation to specify the relationship field or property of the owning side.
[@ManyToMany]:如果关系是双向的,非拥有方必须使用ManyToMany注解的mappedBy元素来指定拥有方的关系字段或属性。
I am having trouble understanding this ownership aspect.我在理解所有权方面有困难。 For example, I have the following associations:
例如,我有以下关联:
Note: Images taken from here.注:图片取自此处。
So which are the non-owning entity sides of these associations?
那么这些协会的非拥有实体方面是哪些?
In the bi-directional relationship betweens two objects ,you have to choose which sides to manage the relationship.在两个对象之间的双向关系中,您必须选择管理关系的边。 From the database perspective , managing the relationship means managing the value of some FK column that link between two tables.
从数据库的角度来看,管理关系意味着管理连接两个表的某些 FK 列的值。 The side that managing it is called owning side.
管理它的一方称为拥有方。 Otherwise, it is call non-owning side.
否则,称为非拥有方。
So back to your example on ProjectManager
and Project
.所以回到你关于
ProjectManager
和Project
例子。 Which object is the owning side depends on which object you choose to manage their relationship.哪个对象是拥有方取决于您选择哪个对象来管理它们的关系。
If you choose ProjectManager
to be the owning side (hence Project
is the non-owning side), only the values of ProjectManager#getProjects()
will be used to determine the value of such FK column.如果您选择
ProjectManager
作为拥有方(因此Project
是非拥有方),则只有ProjectManager#getProjects()
的值将用于确定此类 FK 列的值。 (ie project
table 's project_manager_id
column in this case) The value of Project#getProjectManager()
will be ignored and does not affect the value of this FK column. (即在这种情况下
project
表的project_manager_id
列) Project#getProjectManager()
的值将被忽略并且不会影响此 FK 列的值。
In term of JPA mapping , it is :在 JPA 映射方面,它是:
@Entity
@Table(name="project_manager")
public class ProjectManager{
@OneToMany
private List<Project> projects = new ArrayList<>();
}
@Entity
@Table(name="project")
public class Project {
@ManyToOne
@JoinColumn(name = "project_manager_id")
private ProjectManager projectManager;
}
On the other hands , if you choose Project
to the owning side (hence ProjectManager
is the non-owning side) , only the value of Project#getProjectManager()
will be used to determine the value of this FK column while the value of ProjectManager#getProjects()
will be ignored.另一方面,如果您选择
Project
到拥有方(因此ProjectManager
是非拥有方),则只有Project#getProjectManager()
的值将用于确定此 FK 列的值,而ProjectManager#getProjects()
的值将用于确定此 FK 列的值ProjectManager#getProjects()
将被忽略。 The JPA mapping in the case will be :这种情况下的 JPA 映射将是:
@Entity
@Table(name="project_manager")
public class ProjectManager{
@OneToMany(mappedBy="projectManager")
private List<Project> projects = new ArrayList<>();
}
@Entity
@Table(name="project")
public class Project {
@ManyToOne
@JoinColumn(name = "project_manager_id")
private ProjectManager projectManager;
}
PS: I explain it using property access , hopefully you should get the idea. PS:我使用属性访问来解释它,希望你应该明白。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.