ChatGPT解决这个技术问题 Extra ChatGPT

双向 JPA OneToMany/ManyToOne 关联中的“关联的反面”是什么?

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

所以我的问题基本上是:

在双向(一对多/多对一)关联中,哪个实体是所有者?我们如何将 One 指定为所有者?我们如何将多方指定为所有者? “关联的反面”是什么意思?我们如何将一侧指定为反面?我们如何将多面指定为反面?

您提供的链接已过时。请更新。

A
Aaron Digulla

要理解这一点,你必须退后一步。在 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)的两列表。

这种方式比上面的方式更加灵活(可以轻松处理一对一、多对一、一对多甚至多对多)。价格是这样

它有点慢(必须维护另一个表并且连接使用三个表而不是两个),

连接语法更复杂(如果您必须手动编写许多查询,例如当您尝试调试某些东西时,这可能会很乏味)

它更容易出错,因为当管理连接表的代码出现问题时,您可能会突然得到太多或太少的结果。

这就是为什么我很少推荐这种方法。


只是为了澄清:多方是所有者;一方面是相反的。你别无选择(实际上)。
不,Hibernate 发明了这个。我不喜欢它,因为它将部分实现暴露给 OO 模型。我更喜欢使用 @Parent@Child 注释而不是“XtoY”来说明连接的含义意味着(而不是它是如何实现
@AaronDigulla 每次我必须通过 OneToMany 映射来阅读这个答案时,可能是关于 SO 主题的最佳答案。
哇。如果只有 ORM 框架文档有这么好的解释 - 它会让整个事情更容易理解!优秀的答案!
@klausch:Hibernate 文档令人困惑。忽略它。查看代码、数据库中的 SQL 以及外键是如何工作的。如果你愿意,你可以把智慧带回家:文档是谎言。使用来源,卢克。
S
Steve Jones

令人难以置信的是,在 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
}

唯一的双向映射解决方案是让“多”方拥有指向“一”的指针,并使用@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;
}

在您的单向示例中,JPA 期望存在一个额外的 customer_orders 表。使用 JPA2,您可以在 Customer 的 orders 字段上使用 @JoinColumn 注释(我似乎经常使用)来表示 Order 表中应该使用的数据库外键列。这样,您在 Java 中就有了单向关系,同时在 Order 表中仍然有一个外键列。所以在对象世界中,订单不知道客户,而在数据库世界中,客户不知道订单。
为了更加完整,您可以展示客户是关系拥有方的双向情况。
Z
Zaki

数据库中具有外键表的实体是拥有实体,而指向的另一个表是逆实体。


更简单:所有者是具有 FK 列的表
简单而好的解释。任何一方都可以成为所有者。如果我们在 Order.java 中使用 mappedBy,在 Customer 字段 上将创建一个新表,类似于 Order_Customer,它有 2 列。 ORDER_ID 和 CUSTOMER_ID。
K
Ken Block

双向关系的简单规则:

1.对于多对一的双向关系,多方始终是关系的拥有方。示例:1 个房间有很多人(一个人只属于一个房间)-> 拥有方是人

2.对于一对一的双向关系,拥有方对应于包含对应外键的一方。

3.对于多对多的双向关系,任何一方都可能是拥有方。

希望能帮到你。


为什么我们需要拥有所有者和逆?我们已经有了单面和多面的有意义的概念,在多对多情况下谁是所有者并不重要。决定的后果是什么?很难相信像数据库工程师这样左脑的人决定创造这些多余的概念。
a
andrucz

对于两个实体类 Customer 和 Order ,hibernate 将创建两个表。

可能的情况:

在 Customer.java 和 Order.java 类中不使用 mappedBy then-> 在客户端将创建一个新表 [name = CUSTOMER_ORDER],它将保持 CUSTOMER_ID 和 ORDER_ID 的映射。这些是客户和订单表的主键。在订单端需要一个额外的列来保存对应的 Customer_ID 记录映射。在 Customer.java 中使用了 mappedBy [如问题陈述中给出的] 现在没有创建附加表 [CUSTOMER_ORDER]。 Order.java 中只使用了 Order Table mappedby 中的一列 现在 hibernate 将创建额外的表。[name = CUSTOMER_ORDER] Order Table 将没有额外的列 [Customer_ID] 用于映射。

任何一方都可以成为关系的所有者。但最好选择 xxxToOne 端。

编码效果 -> 只有实体的拥有方可以更改关系状态。在下面的示例 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();
    }
}

V
Vlad Mihalcea

表关系与实体关系

在关系数据库系统中,只能存在三种表关系:

一对多(通过外键列)

一对一(通过共享主键)

多对多(通过一个链接表,其中两个外键引用两个单独的父表)

因此,one-to-many 表关系如下所示:

https://i.stack.imgur.com/rq8rZ.png

请注意,该关系基于子表中的外键列(例如,post_id)。

因此,在管理 one-to-many 表关系时只有一个事实来源。

现在,如果您采用映射到我们之前看到的 one-to-many 表关系的双向实体关系:

https://i.stack.imgur.com/qIb2n.png

如果你看一下上面的图表,你可以看到有两种方法来管理这种关系。

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;

因此,您有两个方面可以更改实体关联:

通过在评论子集合中添加一个条目,一个新的 post_comment 行应该通过其 post_id 列与父帖子实体相关联。

通过设置 PostComment 实体的 post 属性,post_id 列也应该被更新。

因为有两种方式来表示外键列,所以在将关联状态更改转换为其等效的外键列值修改时,您必须定义哪个是事实来源。

MappedBy(又名反面)

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

addCommentremoveComment 方法确保双方同步。因此,如果我们添加一个子实体,则子实体需要指向父实体,并且父实体应该将子实体包含在子集合中。