JPA @Embedded And @Embeddable – JPA @Embedded 和 @Embeddable

最后修改: 2019年 4月 16日

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

1. Overview

1.概述

In this tutorial, we’ll see how we can map one entity that contains embedded properties to a single database table.

在本教程中,我们将看到如何将一个包含嵌入式属性的实体映射到一个数据库表中。

For this purpose, we’ll use the @Embeddable and @Embedded annotations provided by the Java Persistence API (JPA).

为此,我们将使用@Embeddable@Embedded注释,该注释由Java Persistence API(JPA)提供。

2. Data Model Context

2.数据模型的背景

First, let’s define a table called company.

首先,让我们定义一个名为company的表。

The company table will store basic information such as company name, address and phone as well as the information of a contact person:

公司表将存储基本信息,如公司名称、地址和电话,以及联系人的信息。

public class Company {

    private Integer id;

    private String name;

    private String address;

    private String phone;

    private String contactFirstName;

    private String contactLastName;

    private String contactPhone;

    // standard getters, setters
}

However, it seems like the contact person should be abstracted out to a separate class.

然而,联系人似乎应该被抽象为一个单独的类。

The problem is that we don’t want to create a separate table for those details.

问题是,我们不想为这些细节创建一个单独的表。

So, let’s see what we can do.

所以,让我们看看我们能做什么。

3. @Embeddable

3.@Embeddable

JPA provides the @Embeddable annotation to declare that a class will be embedded by other entities.

JPA提供了@Embeddable注解来声明一个类将被其他实体所嵌入。

Let’s define a class to abstract out the contact person details:

让我们定义一个类来抽象出联系人的详细信息。

@Embeddable
public class ContactPerson {

    private String firstName;

    private String lastName;

    private String phone;

    // standard getters, setters
}

4. @Embedded

4.@Embedded

The JPA annotation @Embedded is used to embed a type into another entity.

JPA注解@Embedded用于将一个类型嵌入另一个实体中。

Let’s next modify our Company class.

接下来让我们修改我们的Company类。

We’ll add the JPA annotations, and we’ll also change to use ContactPerson instead of separate fields:

我们将添加JPA注解,并且我们也将改变为使用ContactPerson 而不是单独的字段。

@Entity
public class Company {

    @Id
    @GeneratedValue
    private Integer id;

    private String name;

    private String address;

    private String phone;

    @Embedded
    private ContactPerson contactPerson;

    // standard getters, setters
}

As a result, we have our entity Company, embedding contact person details and mapping to a single database table.

因此,我们有了我们的实体Company,嵌入了联系人的详细信息,并映射到一个数据库表。

We still have one more problem, though, and that is how JPA will map these fields to database columns.

不过我们还有一个问题,那就是JPA将如何把这些字段映射到数据库列。

5. Attributes Override

5.属性覆盖

Our fields were called things like contactFirstName in our original Company class and now firstName in our ContactPerson class. So, JPA will want to map these to contact_first_name and first_name, respectively.

我们的字段在原来的Company类中被称为contactFirstName,现在在ContactPerson类中被称为firstName。所以,JPA将希望把这些分别映射到contact_first_namefirst_name

Aside from being less than ideal, it will actually break us with our now-duplicated phone column.

除了不那么理想之外,它实际上将打破我们现在重复的电话栏目。

So, we can use @AttributeOverrides and @AttributeOverride to override the column properties of our embedded type.

因此,我们可以使用@AttributeOverrides@AttributeOverride来覆盖我们嵌入式类型的列属性。

Let’s add this to the ContactPerson field in our Company entity:

让我们把它添加到我们的公司实体的ContactPerson字段中。

@Embedded
@AttributeOverrides({
  @AttributeOverride( name = "firstName", column = @Column(name = "contact_first_name")),
  @AttributeOverride( name = "lastName", column = @Column(name = "contact_last_name")),
  @AttributeOverride( name = "phone", column = @Column(name = "contact_phone"))
})
private ContactPerson contactPerson;

Note that since these the annotations go on the field, we can have different overrides for each enclosing entity.

请注意,由于这些注释是在字段上的,我们可以对每个包围的实体有不同的重写。

6. Conclusion

6.结语

In this article, we configured an entity with some embedded attributes and mapped them to the same database table as the enclosing entity.

在这篇文章中,我们配置了一个带有一些嵌入式属性的实体,并将它们映射到与包围实体相同的数据库表中。

To do this, we used the @Embedded, @Embeddable, @AttributeOverrides and @AttributeOverride annotations provided by the Java Persistence API.

为此,我们使用了Java Persistence API提供的@Embedded@Embeddable@AttributeOverrides@AttributeOverride注释。

As always, the source code of the example is available over on GitHub.

一如既往,该示例的源代码可在GitHub上获得over