Spring JPA @Embedded and @EmbeddedId – Spring JPA @Embedded和@EmbeddedId

最后修改: 2020年 5月 4日

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

1. Overview

1.概述

In this tutorial, we’re going to cover the use of the @EmbeddedId annotation and “findBy” method for querying a composite key based JPA entity.

在本教程中,我们将介绍使用@EmbeddedId注解和”findBy“方法来查询一个基于复合键的JPA实体。

Hence we’ll be using @EmbeddeId and @Embeddable annotations to represent composite keys in JPA entities. We also need to use Spring JpaRepository to achieve our goal.

因此我们将使用@EmbeddeId@Embeddable注解来表示JPA实体中的复合键。我们还需要使用Spring JpaRepository来实现我们的目标。

We’ll concentrate on querying objects by partial primary key.

我们将专注于按部分主键查询对象。

2. Need for @Embeddable and @EmbeddedId

2.需要@Embeddable@EmbeddedId

In software, we come across many use cases when we need to have a composite primary key to define an entry in a table. Composite primary keys are keys that use more than one column to identify a row in the table uniquely.

在软件中,当我们需要有一个复合主键来定义表中的条目时,我们会遇到很多用例。复合主键是指使用一个以上的列来唯一地识别表中的一行的键

We represent a composite primary key in Spring Data by using the @Embeddable annotation on a class. This key is then embedded in the table’s corresponding entity class as the composite primary key by using the @EmbeddedId annotation on a field of the @Embeddable type.

我们通过在一个类上使用@Embeddable注解来表示Spring Data中的复合主键。然后通过在@Embeddable类型的字段上使用@EmbeddedId注解,将这个键嵌入到表的相应实体类中,作为复合主键。

3. Example

3.例子

Consider a book table, where a book record has a composite primary key consisting of author and name. Sometimes, we might want to find books by a part of the primary key. For example, a user might want to search for books only by a particular author. We’ll learn how to do this with JPA.

考虑一个book表,其中一个book记录有一个由authorname组成的复合主键。有时,我们可能想通过主键的一部分来查找。例如,一个用户可能想只搜索某个特定作者的书。我们将学习如何用JPA来实现这一点。

Our primary application will consist of an @Embeddable BookId and @Entity Book with @EmbeddedId BookId.

我们的主要应用程序将由一个@Embeddable BookId@Entity Book组成,@EmbeddedId BookId.

3.1. @Embeddable

3.1.@Embeddable

Let’s define our BookId class in this section. The author and name will specify a unique BookId — the class is Serializable and implements both equals and hashCode methods:

让我们在这部分定义我们的BookId类。authorname将指定一个唯一的BookId–该类是Serializable并实现了equalshashCodemethods。

@Embeddable
public class BookId implements Serializable {

    private String author;
    private String name;

    // standard getters and setters
}

3.2. @Entity and @EmbeddedId

3.2.@Entity@EmbeddedId

Our Book entity has @EmbeddedId BookId and other fields related to a book. BookId tells JPA that the Book entity has a composite key:

我们的Book实体有@EmbeddedId BookId和其他与book相关的字段。BookId告诉JPA,Book实体有一个复合键。

@Entity
public class Book {

    @EmbeddedId
    private BookId id;
    private String genre;
    private Integer price;

    //standard getters and setters
}

3.3. JPA Repository and Method Naming

3.3.JPA存储库和方法命名

Let us quickly define our JPA repository interface by extending the JpaRepository with entity Book as well as BookId:

让我们通过扩展JpaRepository与实体Book以及BookId来快速定义我们的JPA仓库接口。

@Repository
public interface BookRepository extends JpaRepository<Book, BookId> {

    List<Book> findByIdName(String name);

    List<Book> findByIdAuthor(String author);
}

We use a part of the id variable’s field names to derive our Spring Data query methods. Hence, JPA interprets the partial primary key query as:

我们使用id变量的一部分字段名来派生我们的Spring Data查询方法。因此,JPA将部分主键查询解释为。

findByIdName -> directive "findBy" field "id.name"
findByIdAuthor -> directive "findBy" field "id.author"

4. Conclusion

4.总结

JPA can be used to efficiently map composite keys and query them via derived queries.

JPA可以用来有效地映射复合键并通过派生查询进行查询。

In this article, we saw a small example of running a partial id field search. We looked at the @Embeddable annotation to represent the composite primary key and the @EmbeddedId annotation to insert a composite key in an entity.

在这篇文章中,我们看到了一个运行部分id字段搜索的小例子。我们看了代表复合主键的@Embeddable 注解和在实体中插入复合键的@EmbeddedId 注解。

Finally, we saw how to use the JpaRepository findBy derived methods to search with partial id fields.

最后,我们看到如何使用JpaRepository findBy派生方法来搜索部分id字段。

As always, the example code for this tutorial is available over on GitHub.

一如既往,本教程的示例代码可在GitHub上获得超过