@OneToOne/@ManyToOne/@ManyToMany 的非拥有实体端
Non-owning entity side of @OneToOne/@ManyToOne/@ManyToMany
我正在尝试理解 javax.persistence 注释 @OneToOne, @ManyToOne and @ManyToMany。这些注释的描述提到了 non-owning side
。具体来说:
[@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.
[@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.
[@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.
我无法理解所有权方面的问题。例如,我有以下关联:
注:图片取自here.
So which are the non-owning entity sides of these associations?
在两个对象之间的双向关系中,您必须选择管理关系的一方。从数据库的角度来看,管理关系意味着管理两个 table 之间 link 的某些 FK 列的值。管理它的一方称为拥有方。否则称为非拥有方。
回到你在 ProjectManager
和 Project
上的例子。哪个对象是拥有方取决于您选择哪个对象来管理它们的关系。
如果您选择ProjectManager
作为拥有方(因此Project
是非拥有方),只有ProjectManager#getProjects()
的值将被用于确定的值这样的FK专栏。 (即本例中 project
table 的 project_manager_id
列) Project#getProjectManager()
的值将被忽略,并且不会影响此 FK 列的值。
在 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;
}
另一方面,如果您选择 Project
到拥有方(因此 ProjectManager
是非拥有方),则只有 Project#getProjectManager()
的值将用于确定此 FK 列的值,而 ProjectManager#getProjects()
的值将被忽略。本例中的 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;
}
P.S:我用 property access 来解释它,希望你能明白。
我正在尝试理解 javax.persistence 注释 @OneToOne, @ManyToOne and @ManyToMany。这些注释的描述提到了 non-owning side
。具体来说:
[@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.
[@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.
[@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.
我无法理解所有权方面的问题。例如,我有以下关联:
注:图片取自here.
So which are the non-owning entity sides of these associations?
在两个对象之间的双向关系中,您必须选择管理关系的一方。从数据库的角度来看,管理关系意味着管理两个 table 之间 link 的某些 FK 列的值。管理它的一方称为拥有方。否则称为非拥有方。
回到你在 ProjectManager
和 Project
上的例子。哪个对象是拥有方取决于您选择哪个对象来管理它们的关系。
如果您选择ProjectManager
作为拥有方(因此Project
是非拥有方),只有ProjectManager#getProjects()
的值将被用于确定的值这样的FK专栏。 (即本例中 project
table 的 project_manager_id
列) Project#getProjectManager()
的值将被忽略,并且不会影响此 FK 列的值。
在 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;
}
另一方面,如果您选择 Project
到拥有方(因此 ProjectManager
是非拥有方),则只有 Project#getProjectManager()
的值将用于确定此 FK 列的值,而 ProjectManager#getProjects()
的值将被忽略。本例中的 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;
}
P.S:我用 property access 来解释它,希望你能明白。