在TopLink JPA Annotation Reference上的这些示例中:
示例1-59 @OneToMany - 具有泛型的客户类
@Entity
public class Customer implements Serializable {
...
@OneToMany(cascade=ALL, mappedBy="customer")
public Set<Order> getOrders() {
return orders;
}
...
}
示例1-60 @ManyToOne - 带有泛型的订单类
@Entity
public class Order implements Serializable {
...
@ManyToOne
@JoinColumn(name="CUST_ID", nullable=false)
public Customer getCustomer() {
return customer;
}
...
}
在我看来,Customer
实体是该关联的所有者。但是,在同一文档中对mappedBy
属性的解释中,写成:
如果关系是双向的, 然后在上面设置mappedBy元素 逆(非拥有)的一面 与该字段名称的关联 或拥有这种关系的财产 如例1-60所示。
但是,如果我没有错,看起来在示例中,mappedBy
实际上是在关联的拥有方指定的,而不是非拥有方。
所以我的问题基本上是:
在双向(一对多/多对一)关联中,哪个实体是所有者?我们如何指定一方作为所有者?我们怎样才能将多方指定为所有者?
“协会的反面”是什么意思?我们怎样才能将一面指定为反面?我们怎样才能将多面指定为反面?
答案 0 :(得分:295)
要理解这一点,你必须退后一步。在OO中,客户拥有订单(订单是客户对象中的列表)。没有客户就没有订单。因此,客户似乎是订单的所有者。
但是在SQL世界中,一个项目实际上包含指向另一个项目的指针。由于N个订单有1个客户,因此每个订单都包含其所属客户的外键。这是“连接”,这意味着订单“拥有”(或字面上包含)连接(信息)。这与OO /模型世界完全相反。
这可能有助于理解:
public class Customer {
// This field doesn't exist in the database
// It is simulated with a SQL query
// "OO speak": Customer owns the orders
private List<Order> orders;
}
public class Order {
// This field actually exists in the DB
// In a purely OO model, we could omit it
// "DB speak": Order contains a foreign key to customer
private Customer customer;
}
反面是对象的OO“所有者”,在这种情况下是客户。客户在表中没有用于存储订单的列,因此您必须告诉它在订单表中可以保存此数据的位置(通过mappedBy
发生)。
另一个常见示例是具有节点的树,其可以是父节点和子节点。在这种情况下,两个字段用于一个类:
public class Node {
// Again, this is managed by Hibernate.
// There is no matching column in the database.
@OneToMany(cascade = CascadeType.ALL) // mappedBy is only necessary when there are two fields with the type "Node"
private List<Node> children;
// This field exists in the database.
// For the OO model, it's not really necessary and in fact
// some XML implementations omit it to save memory.
// Of course, that limits your options to navigate the tree.
@ManyToOne
private Node parent;
}
这解释了“外键”多对一设计的作用。还有第二种方法使用另一个表来维持关系。这意味着,对于我们的第一个示例,您有三个表:一个包含客户,一个包含订单,另一个包含两对主表(customerPK,orderPK)。
这种方法比上面的方法更灵活(它可以轻松处理一对一,多对一,一对多,甚至多对多)。价格是那个
这就是为什么我很少推荐这种方法。
答案 1 :(得分:40)
令人难以置信的是,在3年内,没有人用两种方式来回答你的优秀问题。
如其他人所述,“所有者”一侧包含数据库中的指针(外键)。您可以指定任何一方作为所有者,但是,如果您将一方指定为所有者,则该关系将不是双向的(反向又称“许多”方将不知道其“所有者”)。这对封装/松散耦合来说是理想的:
// "One" Customer owns the associated orders by storing them in a customer_orders join table
public class Customer {
@OneToMany(cascade = CascadeType.ALL)
private List<Order> orders;
}
// if the Customer owns the orders using the customer_orders table,
// Order has no knowledge of its Customer
public class Order {
// @ManyToOne annotation has no "mappedBy" attribute to link bidirectionally
}
唯一的双向映射解决方案是让“many”端拥有指向“one”的指针,并使用@OneToMany“mappedBy”属性。如果没有“mappedBy”属性,Hibernate将期望双重映射(数据库将同时具有连接列和连接表,这是多余的(通常是不合需要的))。
// "One" Customer as the inverse side of the relationship
public class Customer {
@OneToMany(cascade = CascadeType.ALL, mappedBy = "customer")
private List<Order> orders;
}
// "many" orders each own their pointer to a Customer
public class Order {
@ManyToOne
private Customer customer;
}
答案 2 :(得分:33)
在数据库中具有带外键的表的实体是拥有实体,而指向的另一个表是反向实体。
答案 3 :(得分:13)
双向关系的简单规则:
1.对于多对一的双向关系,许多方面始终是关系的拥有方。示例:1个房间有很多人(一个人只属于一个房间) - &gt;拥有方是人
2.对于一对一的双向关系,拥有方对应于包含相应外键的一侧。
3.对于多对多双向关系,任何一方都可能是拥有方。
希望可以帮助你。答案 4 :(得分:3)
对于两个实体类Customer和Order,hibernate将创建两个表。
可能的案例:
在Customer.java和Order.java类中没有使用mappedBy - >&gt;
在客户端将创建一个新表[name = CUSTOMER_ORDER],它将保持CUSTOMER_ID和ORDER_ID的映射。这些是客户和订单表的主键。 在订单端,需要一个额外的列来保存相应的Customer_ID记录映射。
在Customer.java中使用mappedBy [在问题陈述中给出] 现在没有创建附加表[CUSTOMER_ORDER]。订单表中只有一列
mappedby用于Order.java 现在,hibernate将创建另外的表。[name = CUSTOMER_ORDER] 订单表将没有用于映射的附加列[Customer_ID]。
任何一方都可以成为这段关系的所有者。但最好选择xxxToOne方面。
编码效果 - &gt;只有实体的所有方可以改变关系状态。在下面的示例中,BoyFriend类是关系的所有者。即使女朋友想分手,她也不能。
import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;
@Entity
@Table(name = "BoyFriend21")
public class BoyFriend21 {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "Boy_ID")
@SequenceGenerator(name = "Boy_ID", sequenceName = "Boy_ID_SEQUENCER", initialValue = 10,allocationSize = 1)
private Integer id;
@Column(name = "BOY_NAME")
private String name;
@OneToOne(cascade = { CascadeType.ALL })
private GirlFriend21 girlFriend;
public BoyFriend21(String name) {
this.name = name;
}
public BoyFriend21() {
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public BoyFriend21(String name, GirlFriend21 girlFriend) {
this.name = name;
this.girlFriend = girlFriend;
}
public GirlFriend21 getGirlFriend() {
return girlFriend;
}
public void setGirlFriend(GirlFriend21 girlFriend) {
this.girlFriend = girlFriend;
}
}
import org.hibernate.annotations.*;
import javax.persistence.*;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.Table;
import java.util.ArrayList;
import java.util.List;
@Entity
@Table(name = "GirlFriend21")
public class GirlFriend21 {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "Girl_ID")
@SequenceGenerator(name = "Girl_ID", sequenceName = "Girl_ID_SEQUENCER", initialValue = 10,allocationSize = 1)
private Integer id;
@Column(name = "GIRL_NAME")
private String name;
@OneToOne(cascade = {CascadeType.ALL},mappedBy = "girlFriend")
private BoyFriend21 boyFriends = new BoyFriend21();
public GirlFriend21() {
}
public GirlFriend21(String name) {
this.name = name;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public GirlFriend21(String name, BoyFriend21 boyFriends) {
this.name = name;
this.boyFriends = boyFriends;
}
public BoyFriend21 getBoyFriends() {
return boyFriends;
}
public void setBoyFriends(BoyFriend21 boyFriends) {
this.boyFriends = boyFriends;
}
}
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import java.util.Arrays;
public class Main578_DS {
public static void main(String[] args) {
final Configuration configuration = new Configuration();
try {
configuration.configure("hibernate.cfg.xml");
} catch (HibernateException e) {
throw new RuntimeException(e);
}
final SessionFactory sessionFactory = configuration.buildSessionFactory();
final Session session = sessionFactory.openSession();
session.beginTransaction();
final BoyFriend21 clinton = new BoyFriend21("Bill Clinton");
final GirlFriend21 monica = new GirlFriend21("monica lewinsky");
clinton.setGirlFriend(monica);
session.save(clinton);
session.getTransaction().commit();
session.close();
}
}
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import java.util.List;
public class Main578_Modify {
public static void main(String[] args) {
final Configuration configuration = new Configuration();
try {
configuration.configure("hibernate.cfg.xml");
} catch (HibernateException e) {
throw new RuntimeException(e);
}
final SessionFactory sessionFactory = configuration.buildSessionFactory();
final Session session1 = sessionFactory.openSession();
session1.beginTransaction();
GirlFriend21 monica = (GirlFriend21)session1.load(GirlFriend21.class,10); // Monica lewinsky record has id 10.
BoyFriend21 boyfriend = monica.getBoyFriends();
System.out.println(boyfriend.getName()); // It will print Clinton Name
monica.setBoyFriends(null); // It will not impact relationship
session1.getTransaction().commit();
session1.close();
final Session session2 = sessionFactory.openSession();
session2.beginTransaction();
BoyFriend21 clinton = (BoyFriend21)session2.load(BoyFriend21.class,10); // Bill clinton record
GirlFriend21 girlfriend = clinton.getGirlFriend();
System.out.println(girlfriend.getName()); // It will print Monica name.
//But if Clinton[Who owns the relationship as per "mappedby" rule can break this]
clinton.setGirlFriend(null);
// Now if Monica tries to check BoyFriend Details, she will find Clinton is no more her boyFriend
session2.getTransaction().commit();
session2.close();
final Session session3 = sessionFactory.openSession();
session1.beginTransaction();
monica = (GirlFriend21)session3.load(GirlFriend21.class,10); // Monica lewinsky record has id 10.
boyfriend = monica.getBoyFriends();
System.out.println(boyfriend.getName()); // Does not print Clinton Name
session3.getTransaction().commit();
session3.close();
}
}
答案 5 :(得分:2)
在关系数据库系统中,只能存在三种类型的表关系:
因此,one-to-many
table relationship如下所示:
请注意,该关系基于子表中的外键列(例如post_id
)。
因此,在管理one-to-many
表关系时,只有一个事实来源。
现在,如果您采用的双向实体关系映射到我们之前见过的one-to-many
表关系:
看看上面的图,您会发现有两种方法可以管理这种关系。
在Post
实体中,您拥有comments
集合:
@OneToMany(
mappedBy = "post",
cascade = CascadeType.ALL,
orphanRemoval = true
)
private List<PostComment> comments = new ArrayList<>();
并且,在PostComment
中,post
关联被映射如下:
@ManyToOne(
fetch = FetchType.LAZY
)
@JoinColumn(name = "post_id")
private Post post;
因此,您有两个方面可以更改实体关联:
comments
子集合中添加条目,新的post_comment
行应通过其post
列与父post_id
实体相关联。post
实体的PostComment
属性,post_id
列也应更新。因为有两种表示外键列的方法,所以在将关联状态更改转换为其等效的外键列值修改时,必须定义哪个是真相的来源。
mappedBy
属性告诉@ManyToOne
端负责管理外键列,并且该集合仅用于获取子实体并将父实体状态更改级联为子实体(例如,删除父实体也应该删除子实体。
之所以称为反面,是因为它引用了管理此表关系的子实体属性。
现在,即使您定义了mappedBy
属性,并且子端@ManyToOne
关联管理“外键”列,您仍然需要同步双向关联的两端。
做到这一点的最佳方法是添加以下两个实用程序方法:
public void addComment(PostComment comment) {
comments.add(comment);
comment.setPost(this);
}
public void removeComment(PostComment comment) {
comments.remove(comment);
comment.setPost(null);
}
addComment
和removeComment
方法可确保双方同步。因此,如果我们添加一个子实体,则该子实体需要指向父实体,并且该父实体应在子集合中包含该子实体。
有关同步所有双向实体关联类型的最佳方法的更多详细信息,请查看this article。