在双向JPA OneToMany / ManyToOne关联中,“关联的反面”是什么意思? [英] In a bidirectional JPA OneToMany/ManyToOne association, what is meant by "the inverse side of the association"?

查看:155
本文介绍了在双向JPA OneToMany / ManyToOne关联中,“关联的反面”是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这些 JPA注释参考链接



示例1-59 @OneToMany - 带泛型的客户类

  @Entity 
public class Customer实现Serializable {
...
@OneToMany(cascade = ALL,mappedBy =customer)
public Set< Order> getOrders(){
返回订单;
}
...
}

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 实际上是在关联的拥有者一侧指定的,而不是非拥有者一方。



所以我的问题基本上是:在一个双向(一对多/多对一)关联,哪个实体是所有者?我们如何指定一方作为所有者?我们如何指定多方作为所有者? <协会的反面意味着什么?我们如何能够将一方指定为逆?我们如何指定多方为逆?


解决方案

这,你必须退后一步。在OO中,客户拥有订单(订单是客户对象中的清单)。没有客户就不可能有订单。所以客户似乎是订单的所有者。



但是在SQL世界中,一个项目实际上会包含一个指向另一个的指针。由于N个订单有1个客户,因此每个订单都包含它所属客户的外键。这是连接,这意味着命令拥有(或字面上包含)连接(信息)。这与OO /模特界完全相反。



这可能有助于理解:

  public class Customer {
//数据库中不存在此字段
//使用SQL查询
//OO speak来模拟:客户拥有订单
私人列表<订单>命令;
}

public class Order {
//该字段实际存在于DB
中//在纯粹的OO模型中,我们可以省略
//DB speak:订单包含客户的外键
私人客户客户;

$ / code>

反面是对象的OO所有者,在这种情况下客户。客户在表中没有列来存储订单,所以您必须告诉它在订单表中的哪个位置可以保存这些数据(这通过 mappedBy )发生。 p>

In these examples on TopLink JPA Annotation Reference:

Example 1-59 @OneToMany - Customer Class With Generics

@Entity
public class Customer implements Serializable {
    ...
    @OneToMany(cascade=ALL, mappedBy="customer")
    public Set<Order> getOrders() { 
        return orders; 
    }
    ...
}

Example 1-60 @ManyToOne - Order Class With Generics

@Entity
public class Order implements Serializable {
    ...
    @ManyToOne
    @JoinColumn(name="CUST_ID", nullable=false)
    public Customer getCustomer() { 
        return customer; 
    }
    ...
}

It seems to me that the Customer entity is the owner of the association. However, in the explanation for the mappedBy attribute in the same document, it is written that:

if the relationship is bidirectional, then set the mappedBy element on the inverse (non-owning) side of the association to the name of the field or property that owns the relationship as Example 1-60 shows.

However, if I am not wrong, it looks like in the example, the mappedBy is actually specified on the owning side of the association, rather than the non-owning side.

So my question is basically:

  1. In a bidirectional (one-to-many/many-to-one) association, which of the entities is the owner? How can we designate the One side as the owner? How can we designate the Many side as the owner?

  2. What is meant by "the inverse side of the association"? How can we designate the One side as the inverse? How can we designate the Many side as the inverse?

解决方案

To understand this, you must take a step back. In OO, the customer owns the orders (orders are a list in the customer object). There can't be an order without a customer. So the customer seems to be the owner of the orders.

But in the SQL world, one item will actually contain a pointer to the other. Since there is 1 customer for N orders, each order contains a foreign key to the customer it belongs to. This is the "connection" and this means the order "owns" (or literally contains) the connection (information). This is exactly the opposite from the OO/model world.

This may help to understand:

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;
}

The inverse side is the OO "owner" of the object, in this case the customer. The customer has no columns in the table to store the orders, so you must tell it where in the order table it can save this data (which happens via mappedBy).

这篇关于在双向JPA OneToMany / ManyToOne关联中,“关联的反面”是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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