The Difference Between JPA, Hibernate and EclipseLink – JPA、Hibernate和EclipseLink之间的区别

最后修改: 2018年 11月 23日

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

1. Introduction

1.绪论

In this tutorial, we’ll be discussing Hibernate and the Java Persistence API (JPA) – with a focus on the differences between them.

在本教程中,我们将讨论Hibernate和Java Persistence API(JPA)–重点是它们之间的区别。

We’ll start by exploring what JPA is, how it’s used, and the core concepts behind it.

我们将从开始,探讨什么是JPA,如何使用它,以及它背后的核心概念

Then, we’ll take a look at how Hibernate and EclipseLink fit into the picture.

然后,我们将看看Hibernate和EclipseLink是如何发挥作用的。

2. Object-Relational Mapping

2.对象-关系映射

Before we dive into JPA, it’s important to understand the concept of Object-Relational Mapping – also known as ORM.

在我们深入研究JPA之前,了解对象-关系映射的概念很重要–也被称为ORM。

Object-relational mapping is simply the process of persisting any Java object directly to a database table. Usually, the name of the object being persisted becomes the name of the table, and each field within that object becomes a column. With the table set up, each row corresponds to a record in the application.

对象关系映射只是将任何Java对象直接持久化到数据库表中的过程。通常,被持久化的对象的名称成为表的名称,该对象中的每个字段成为一个列。随着表的设置,每一行都对应于应用程序中的一条记录。

3. An Introduction to JPA

3.JPA简介

The Java Persistence API, or JPA, is a specification that defines the management of relational data in a Java application. The API maps out a set of concepts that defines which objects within the application should be persisted, and how it should persist them.

Java Persistence API,即JPA,是一个定义了Java应用程序中关系数据管理的规范。该API映射出一组概念,定义了应用程序中哪些对象应该被持久化,以及应该如何持久化。

It’s important to note here that JPA is only a specification and that it needs an implementation to work – but more on that later.

这里需要注意的是,JPA只是一个规范,它需要一个实现才能发挥作用–但后面会有更多关于这个问题的

Now, let’s discuss some of the core JPA concepts that an implementation must cover.

现在,让我们来讨论一些实现必须涵盖的核心JPA概念。

3.1. Entity

3.1. 实体

The javax.persistence.Entity class defines which objects should be persisted to the database. For each persisted entity, JPA creates a new table within the chosen database.

javax.persistence.Entity类定义了哪些对象应该被持久化到数据库中。对于每个被持久化的实体,JPA在所选的数据库中创建一个新的表。

In addition, all chosen entities should define a primary key denoted by the @Id annotation. Together with the @GeneratedValue annotation, we define that the primary key should be automatically generated when the record is persisted to the database.

此外,所有被选中的实体都应该定义一个由@Id注释表示的主键。与@GeneratedValue注解一起,我们定义主键应该在记录被持久化到数据库时自动生成。

Let’s take a look a quick example of an entity described by JPA.

让我们看一下JPA描述的实体的一个快速例子。

@Entity
public class Car {
  
    @GeneratedValue
    @Id
    public long id;

    // getters and setters

}

Remember, this will currently have no effect on the application – JPA doesn’t provide any implementation code.

请记住,这目前不会对应用程序产生任何影响–JPA不提供任何实现代码。

3.2. Field Persistence

3.2.田野持久性

Another core concept of JPA is field persistence. When an object in Java is defined as an entity, all fields within it are automatically persisted as different columns within the entity table.

JPA的另一个核心概念是字段持久化。当Java中的一个对象被定义为一个实体时,其中的所有字段会自动被持久化为实体表中的不同列。

If there’s a field within a persisted object that we don’t want to persist to the database, we can declare the field transient with the @Transient annotation.

如果在一个持久化的对象中,有一个字段我们不想持久化到数据库中,我们可以用@Transient注解声明这个字段是暂时的。

3.3. Relationships

3.3.关系

Next, JPA specifies how we should manage relationships between different database tables within our application. As we’ve seen, JPA handles this with annotations. There are four relationship annotations that we need to keep in mind:

接下来,JPA规定了我们应该如何管理应用程序中不同数据库表之间的关系。正如我们所看到的,JPA用注解来处理这个问题。有四个关系注解需要我们牢记。

  1. @OneToOne
  2. @OneToMany
  3. @ManyToOne
  4. @ManyToMany

Let’s take a look at how this works:

让我们来看看这是如何运作的。

@Entity
public class SteeringWheel {

    @OneToOne
    private Car car

    // getters and setters
}

In our example above, the SteeringWheel class describes a one to one relationship with our Car class from earlier.

在我们上面的例子中,SteeringWheel类描述了与我们前面的Car类的一对一关系。

3.4. Entity Manager

3.4.实体管理器

Finally, the javax.persistence.EntityManager class specifies operations to and from the database. The EntityManager contains common Create, Read, Update and Delete (CRUD) operations that are persisted to the database.

最后,javax.persistence.EntityManager类规定了进出数据库的操作。EntityManager包含常见的创建、读取、更新和删除(CRUD)操作,这些操作被persisted到数据库中。

4. JPA Implementations

4.JPA的实施

With JPA specification defining how and what we should persist, we now need to choose an implementation provider to supply the necessary code. Without such a provider, we would need to implement all of the relevant classes to conform with JPA, and that’s a lot of work!

由于JPA规范定义了我们应该如何持久化以及持久化什么,我们现在需要选择一个实现提供者来提供必要的代码。如果没有这样的提供者,我们将需要实现所有相关的类以符合JPA,而这是一项很多的工作

There are plenty of providers to choose from, with each displaying its own pros and cons. When making a decision on which to use, we should consider a few of the following points:

有很多供应商可供选择,每个供应商都显示出自己的优点和缺点。在决定使用哪一个时,我们应该考虑以下几点

  1. Project maturity – how long has the provider been around, and how well documented is it?
  2. Subprojects – does the provider have any useful subprojects for our new application?
  3. Community support – is there anyone to help us out when we end up with a critical bug?
  4. Benchmarking – how performant is the implementation?

While we won’t be going into depth on the benchmarking of different JPA providers, JPA Performance Benchmark (JPAB) contains valuable insight into this.

虽然我们不会深入研究不同JPA提供商的基准,但JPA性能基准(JPAB)包含了对此的宝贵见解。

With that out of the way, let’s take a brief look at some of the top providers of JPA.

说完这些,让我们简单看看JPA的一些顶级供应商。

5. Hibernate

5.冬眠

At its core, Hibernate is an object-relational mapping tool that provides an implementation of JPAHibernate is one of the most mature JPA implementations around, with a huge community backing the project.

在其核心中,Hibernate是一个对象关系映射工具,提供了JPA的实现Hibernate是最成熟的JPA实现之一,有一个庞大的社区在支持该项目。

It implements all of the javax.persistence classes we looked at earlier in the article as well as providing functionality beyond JPA – Hibernate tools, validation, and search. Although these Hibernate-specific APIs may be useful, they are not needed in applications that only require the base JPA functionality.

它实现了我们在文章前面看到的所有javax.persistence类,并提供了JPA以外的功能 – Hibernate工具验证搜索。尽管这些针对Hibernate的API可能很有用,但在只需要基本JPA功能的应用程序中不需要它们。

Let’s take a quick look at what Hibernate offers with the @Entity annotation.

让我们快速浏览一下Hibernate提供的@Entity注解的内容。

Whilst fulfilling JPA contract, @org.hibernate.annotations.Entity adds additional metadata that goes beyond JPA specification. Doing so allows fine-tuning entity persistence. For example, let’s look at a few annotations offered by Hibernate that extends the functionality of @Entity:

在履行JPA契约的同时,@org.hibernate.annotations.Entity添加了超越JPA规范的额外元数据。这样做可以对实体持久性进行微调。例如,让我们看看Hibernate提供的几个注释,它们扩展了@Entity的功能:

  1. @Table allows us to specify the name of the table created for the entity
  2. @BatchSizespecifies the batch size when retrieving entities from the table

It’s also worth noting a few of the extra features that the JPA does not specify, that may prove useful in larger applications:

还值得注意的是,JPA没有指定的一些额外的功能,这些功能可能在更大的应用程序中被证明是有用的。

  1. Customizable CRUD statements with the @SQLInsert, @SQLUpate and @SQLDelete annotations
  2. Support for soft deleting
  3. Immutable entities with the @Immutable annotation

For a deeper dive into Hibernate and Java persistence – head over to our Spring persistence tutorial series.

要深入了解Hibernate和Java持久性–请访问我们的Spring持久性教程系列

6. EclipseLink

6.EclipseLink

EclipseLink, built by the Eclipse Foundation, provides an open-sourced JPA implementation. Additionally, EclipseLink supports a number of other persistence standards such as Java Architecture for XML Binding (JAXB).

EclipseLink由Eclipse基金会构建,提供了一个开源的JPA实现。此外,EclipseLink还支持其他一些持久化标准,如Java Architecture for XML Binding (JAXB)

Simply put, rather than persisting an object to a database row, JAXB maps it to an XML representation.

简单地说,JAXB不是把一个对象持久化为数据库行,而是把它映射为一个XML表示。

Next, by comparing the same @Entity annotation implementation, we see that EclipseLink offers again different extensions. Whilst there is no annotation for @BatchSize as we saw earlier, EclipseLink offers other options that Hibernate doesn’t.

接下来,通过比较相同的@Entity注解实现,我们看到EclipseLink再次提供不同的扩展。虽然没有我们之前看到的@BatchSize注解,EclipseLink提供了Hibernate没有的其他选项。

For example:

比如说。

  1. @ReadOnly – specifies the entity to be persisted is read-only
  2. @Struct – defines the class to map to a database ‘struct’ type

To read more about what EclipseLink has to offer, head over to our guide on EclipseLink with Spring.

要阅读更多关于EclipseLink的内容,请前往我们的关于EclipseLink与Spring的指南

7. Conclusion

7.结语

In this article, we’ve looked at the Java Persistence API, or JPA.

在这篇文章中,我们考察了Java Persistence API,即JPA.

Finally, we explored how it differs from Hibernate and EclipseLink.

最后,我们探讨了它与Hibernate和EclipseLink的区别。