JPA、Hibernate、EclipseLinkの違い

1. 前書き

このチュートリアルでは、HibernateとJava Persistence API(JPA)について、それらの違いに焦点を当てて説明します。

その背後にstart by exploring what JPA is, how it’s used, and the core conceptsします。

次に、HibernateとEclipseLinkがどのように画像に適合するかを見ていきます。

2. オブジェクトリレーショナルマッピング

JPAに飛び込む前に、オブジェクトリレーショナルマッピング(ORMとも呼ばれます)の概念を理解することが重要です。

オブジェクトリレーショナルマッピングは単純にthe process of persisting any Java object directly to a database tableです。 通常、永続化されるオブジェクトの名前はテーブルの名前になり、そのオブジェクト内の各フィールドは列になります。 テーブルを設定すると、各行はアプリケーションのレコードに対応します。

3. JPAの概要

Java Persistence API(JPA)は、Javaアプリケーションでのリレーショナルデータの管理を定義する仕様です。 APIは、アプリケーション内のどのオブジェクトを永続化するか、およびどのようにそれらを永続化するかを定義する一連の概念を作成します。

ここで重要なのは、JPA is only a specification and that it needs an implementation to workですが、on that laterの数が多いことです。

それでは、実装がカバーしなければならないJPAのコア概念のいくつかについて説明しましょう。

3.1. エンティティ

javax.persistence.Entity class defines which objects should be persisted to the database。 永続エンティティごとに、JPAは選択したデータベース内に新しいテーブルを作成します。

さらに、選択したすべてのエンティティは、@Id annotationで示される主キーを定義する必要があります。 @GeneratedValue annotationとともに、レコードがデータベースに永続化されるときに主キーが自動的に生成されるように定義します。

JPAによって記述されたエンティティの簡単な例を見てみましょう。

@Entity
public class Car {

    @GeneratedValue
    @Id
    public long id;

    // getters and setters

}

現在、これはアプリケーションに影響を与えないことに注意してください。JPAは実装コードを提供していません。

3.2. フィールドの永続性

Another core concept of JPA is field persistence。 Javaのオブジェクトがエンティティとして定義されている場合、その中のすべてのフィールドは、エンティティテーブル内の異なる列として自動的に永続化されます。

永続化されたオブジェクト内に、don’t がデータベースに永続化することを望んでいるフィールドがある場合、@Transient annotationを使用してフィールドを一時的に宣言できます。

3.3. 関係

次に、アプリケーション内のJPA specifies how we should manage relationships between different database tables。 これまで見てきたように、JPAはこれをアノテーションで処理します。 留意すべき4つの関係注釈があります。

  1. @OneToOne

  2. しゅう

  3. しゅう

  4. あずきっく

これがどのように機能するかを見てみましょう。

@Entity
public class SteeringWheel {

    @OneToOne
    private Car car

    // getters and setters
}

上記の例では、SteeringWheel classは、以前のCar classとの1対1の関係を表しています。

3.4. エンティティマネージャー

最後に、javax.persistence.EntityManager class は、データベースとの間の操作を指定します。 データベースに対するpersistedであるEntityManager contains common Create, Read, Update and Delete (CRUD) operations

4. JPAの実装

永続化する方法と内容を定義するJPA仕様により、need to choose an implementation provider to supply the necessary codeになりました。 このようなプロバイダーがなければ、JPAに準拠するために、関連するすべてのクラスを実装する必要があります。これは、lotの作業です。

選択できるプロバイダーはたくさんあり、それぞれに長所と短所があります。 使用するWhen making a decisionconsider a few of the following points

  1. プロジェクトの成熟度–*how long has the provider been around*, and how well documented is it?

  2. サブプロジェクト–*does the provider have any useful subprojects* for our new application?

  3. コミュニティサポート–is there anyone to help us out when we end up with a critical bug?

  4. ベンチマーク–*how performant* is the implementation?

さまざまなJPAプロバイダーのベンチマークについては詳しく説明しませんが、JPA Performance Benchmark (JPAB)にはこれに関する貴重な洞察が含まれています。

それが邪魔にならないように、JPAのトッププロバイダーのいくつかを簡単に見てみましょう。

5. 休止状態

その核となるのは、Hibernate is an object-relational mapping tool that provides an implementation of JPAです。 Hibernate is one of the most mature JPA implementationsが周りにあり、巨大なコミュニティがプロジェクトを支援しています。

これは、この記事の前半で説明したすべてのjavax.persistence classを実装し、JPAを超える機能(Hibernate toolsvalidation、およびsearch)を提供します。 これらのHibernate固有のAPIは有用かもしれませんが、基本のJPA機能のみを必要とするアプリケーションでは必要ありません。

Hibernateが@Entityアノテーションで提供するものを簡単に見てみましょう。

JPA契約を履行している間、@org.hibernate.annotations.Entity adds additional metadata that goes beyond JPA specification.を実行すると、エンティティの永続性を微調整できます。 たとえば、Hibernate that extends the functionality of @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

また、JPAで指定されていないいくつかの追加機能にも注目してください。これらは、大規模なアプリケーションで役立つ可能性があります。

  1. @SQLInsert, @SQLUpate and @SQLDelete annotationsを使用したカスタマイズ可能なCRUDステートメント

  2. ソフト削除のサポート

  3. @Immutable annotationを持つ不変エンティティ

HibernateとJavaの永続性についてさらに詳しく知りたい場合は、Spring persistence tutorial seriesに進んでください。

Eclipse Foundationによって構築されたEclipseLinkprovides an open-sourced JPA implementation。 さらに、EclipseLinksupports a number of other persistence standards such as Java Architecture for XML Binding (JAXB)

簡単に言えば、JAXBはオブジェクトをデータベース行に永続化するのではなく、オブジェクトをXML表現にマッピングします。

次に、同じ@Entityアノテーションの実装を比較すると、EclipseLinkが再びdifferent extensionsを提供していることがわかります。 前に見たように@BatchSize の注釈はありませんが、EclipseLink offers other options that Hibernate doesn’t.

例えば:

  1. @ReadOnly –永続化するエンティティが読み取り専用であることを指定します

  2. @Struct – defines the class to map to a database ‘struct' type

EclipseLinkが提供するものの詳細については、guide on EclipseLink with Springにアクセスしてください。

7. 結論

この記事では、looked at the Java Persistence API, or JPA.

最後に、how it differs from Hibernate and EclipseLink.を調べました