在这些示例中,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
属性的解释中,写为:
如果关系是双向的,然后在的反(非所有权)面与字段名称的关联或拥有关系的财产如示例1-60所示。
但是,如果我没记错,在示例中看起来mappedBy
实际上是在关联的拥有方而不是非拥有方指定的。
所以我的问题基本上是:
在双向(一对多/多对一)关联中,所有者是哪个实体?我们如何指定一方为所有者?我们如何指定多方为所有者?
“关联的反面是什么意思?我们如何将一侧指定为逆?我们如何指定多边为逆?
但是在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)的两列表。此方法比上面的方法更灵活(它可以轻松处理一对一,多对一,一对多,甚至多对多)。价格是