Difference Between @JoinColumn and mappedBy – @JoinColumn和mappedBy之间的区别

最后修改: 2018年 11月 21日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

JPA Relationships can be either unidirectional or bidirectional. This simply means we can model them as an attribute on exactly one of the associated entities or both.

JPA关系可以是单向的也可以是双向的。这仅仅意味着我们可以将它们建模为相关实体中的一个属性,或者两者都是。

Defining the direction of the relationship between entities has no impact on the database mapping. It only defines the directions in which we use that relationship in our domain model.

定义实体之间关系的方向对数据库映射没有影响。它只是定义了我们在领域模型中使用这种关系的方向。

For a bidirectional relationship, we usually define

对于一个双向关系,我们通常定义为

  • the owning side
  • inverse or the referencing side

The @JoinColumn annotation helps us specify the column we’ll use for joining an entity association or element collection. On the other hand, the mappedBy attribute is used to define the referencing side (non-owning side) of the relationship.

@JoinColumn注解帮助我们指定我们将用于连接实体关联或元素集合的列。另一方面,mappedBy属性被用来定义关系的引用方(非拥有方)。

In this quick tutorial, we’ll look at the difference between @JoinColumn and mappedBy in JPA. We’ll also present how to use them in a one-to-many association.

在这个快速教程中,我们将看看@JoinColumnmappedBy 在JPA中的区别。我们还将介绍如何在一对多的关联中使用它们。

2. Initial Setup

2.初始设置

To follow along with this tutorial, let’s say we have two entities: Employee and Email.

为了跟上这个教程,让我们说我们有两个实体。EmployeeEmail

Clearly, an employee can have multiple email addresses. However, a given email address can belong exactly to a single employee.

很明显,一个员工可以有多个电子邮件地址。然而,一个特定的电子邮件地址可以完全属于一个雇员。

This means they share a one-to-many association:

这意味着它们共享一个一对多的关联。

Employee - Email

Also in our RDBMS model, we’ll have a foreign key employee_id in our Email entity referring to the id attribute of an Employee.

同样在我们的RDBMS模型中,我们将在我们的Email实体中拥有一个外键employee_id,指的是Employeeid属性。

3. @JoinColumn Annotation

3.@JoinColumn注释

In a One-to-Many/Many-to-One relationship, the owning side is usually defined on the many side of the relationship. It’s usually the side that owns the foreign key.

在一对多/多对一的关系中,拥有方通常被定义在关系的many一侧。它通常是拥有外键的一方。

The @JoinColumn annotation defines that actual physical mapping on the owning side:

@JoinColumn注解定义了在拥有方的实际物理映射。

@Entity
public class Email {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "employee_id")
    private Employee employee;

    // ...

}

It simply means that our Email entity will have a foreign key column named employee_id referring to the primary attribute id of our Employee entity.

这只是意味着我们的Email实体将有一个名为employee_id的外键列,引用我们的Employee实体的主属性id

4. mappedBy Attribute

4.mappedBy属性

Once we have defined the owning side of the relationship, Hibernate already has all the information it needs to map that relationship in our database.

一旦我们定义了关系的拥有方,Hibernate就已经拥有了在我们的数据库中映射这种关系所需的所有信息。

To make this association bidirectional, all we’ll have to do is to define the referencing side. The inverse or the referencing side simply maps to the owning side.

为了使这种关联是双向的,我们所要做的就是定义引用方。逆向或引用方只需映射到拥有方。

We can easily use the mappedBy attribute of @OneToMany annotation to do so.

我们可以很容易地使用mappedBy属性的@OneToMany注解来做到这一点。

So, let’s define our Employee entity:

所以,让我们定义我们的Employee实体。

@Entity
public class Employee {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "employee")
    private List<Email> emails;
    
    // ...
}

Here, the value of mappedBy is the name of the association-mapping attribute on the owning side. With this, we have now established a bidirectional association between our Employee and Email entities.

这里,mappedBy的值是拥有方的关联映射属性的名称。这样,我们现在已经在我们的EmployeeEmail实体之间建立了一个双向关联。

5. Conclusion

5.总结

In this article, we looked at the difference between @JoinColumn and mappedBy and how to use them in a one-to-many bidirectional relationship.

在这篇文章中,我们研究了@JoinColumnmappedBy之间的区别,以及如何在一对多的双向关系中使用它们。

The @JoinColumn annotation defines the actual physical mapping on the owning side. On the other hand, the referencing side is defined using the mappedBy attribute of the @OneToMany annotation.

@JoinColumn 注解定义了拥有一方的实际物理映射。另一方面,引用方是使用@OneToMany 注解的mappedBy 属性来定义的。

As usual, the source code is available over on GitHub.

像往常一样,源代码可在GitHub上获得。