Movatterモバイル変換


[0]ホーム

URL:


Getting Started with Hibernate

version 7.2.0.CR3
Table of Contents

Preface

Hibernate is anObject/Relational Mapping (ORM) solution for programs written in Java and other JVMlanguages.

While a strong background in SQL is not required to use Hibernate, a basic understanding of its concepts is useful - especially the principles ofdata modeling.Understanding the basics of transactions and design patterns such asUnit of Work are important as well.

Useful background resources

1. Obtaining Hibernate

Hibernate is broken into a number of modules/artifacts under theorg.hibernate.ormgroup. The main artifact is namedhibernate-core.

This guide uses 7.2.0.CR3 as the Hibernate version for illustration purposes. Be sure to changethis version, if necessary, to the version you wish to use.

We can declare a dependency on this artifact usingGradle

dependencies{implementation"org.hibernate.orm:hibernate-core:7.2.0.CR3"}

orMaven:

<dependency><groupId>org.hibernate.orm</groupId><artifactId>hibernate-core</artifactId><version>7.2.0.CR3</version></dependency>

1.1. Hibernate ORM modules

As mentioned earlier, Hibernate ORM is broken into a number of modules with the intent of isolatingtransitive dependencies based on the features being used or not.

Table 1.1: API-oriented modules

hibernate-core

The core object/relational mapping engine

hibernate-envers

Entity versioning and auditing

hibernate-spatial

Support for spatial/GIS data types usingGeoLatte

hibernate-processor

An annotation processor that generates a JPA-compliant metamodel, plus optional Hibernate extras

hibernate-vector

Support for mathematical vector types and functions useful for AI/ML topics like vector similarity search and Retrieval-Augmented Generation (RAG)

Table 1.2: Integration-oriented modules

hibernate-agroal

Support forAgroal connection pooling

hibernate-c3p0

Support forC3P0 connection pooling

hibernate-hikaricp

Support forHikariCP connection pooling

hibernate-jcache

Integration withJCache, allowing any compliant implementation as a second-level cache provider

hibernate-graalvm

Experimental extension to make it easier to compile applications as aGraalVM native image

hibernate-micrometer

Integration withMicrometer metrics

hibernate-community-dialects

Additionalcommunity-supported SQL dialects

Table 1.3: Testing-oriented modules

hibernate-testing

A series of JUnit extensions for testing Hibernate ORM functionality

Table 1.4: Third-party modules

Third-party modules, and in particularthird-party dialects, are tested by their own authors,and may not be compatible with all versions of Hibernate ORM.

  1. Check the module’s own documentation to know more about its compatibility constraints.Thecompatibility matrix on the Hibernate website may also be of help.

  2. Submit any question or bug reports about these dialects to the dialect’s authors: the Hibernate team cannot help.

org.mongodb:mongodb-hibernate:{version}

MongoDB Extension for Hibernate ORM

com.google.cloud:google-cloud-spanner-hibernate-dialect:{version}

Google Cloud Spanner Dialect for Hibernate ORM

1.2. Platform / BOM

Hibernate also provides a platform (BOM in Maven terminology) module which can be used to align versions of the Hibernate modules along with the versions of its libraries. The platform artifact is namedhibernate-platform.

To apply the platform in Gradle

dependencies{implementationplatform"org.hibernate.orm:hibernate-platform:7.2.0.CR3"// use the versions from the platformimplementation"org.hibernate.orm:hibernate-core"implementation"jakarta.transaction:jakarta.transaction-api"}

See theGradle documentation for capabilities of applying a platform.

To apply the platform (BOM) in Maven

<dependency><groupId>org.hibernate.orm</groupId><artifactId>hibernate-core</artifactId></dependency><dependency><groupId>jakarta.transaction</groupId><artifactId>jakarta.transaction-api</artifactId></dependency><dependencyManagement><dependencies><dependency><groupId>org.hibernate.orm</groupId><artifactId>hibernate-platform</artifactId><version>7.2.0.CR3</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement>

1.3. Example sources

The bundled examples mentioned in this tutorial can be downloaded fromhere.

Alternatively, the example source code can also be obtained fromGithub

2. Tutorial using native Hibernate APIs

Objectives
  • Configure Hibernate usinghibernate.properties

  • Create aSessionFactory usingnative bootstrapping

  • Use annotations to provide mapping information

  • UseSession to persist and query data

This tutorial is located within the download bundle underannotations/.

2.1. Configuration via properties file

In this example, configuration properties are specified in a file namedhibernate.properties.

Configuration viahibernate.properties
# Database connection settingshibernate.connection.url=jdbc:h2:mem:db1;DB_CLOSE_DELAY=-1hibernate.connection.username=sahibernate.connection.password=# Echo all executed SQL to consolehibernate.show_sql=truehibernate.format_sql=truehibernate.highlight_sql=true# Automatically export the schemahibernate.hbm2ddl.auto=create

The following properties specify JDBC connection information:

Table 2.1: JDBC connection settings
Configuration property namePurpose

jakarta.persistence.jdbc.url

JDBC URL of your database

jakarta.persistence.jdbc.user andjakarta.persistence.jdbc.password

Your database credentials

These tutorials use the H2 embedded database, so the values of these properties are specific to running H2 in its in-memory mode.

These properties enable logging of SQL to the console as it is executed, in an aesthetically pleasing format:

Table 2.2: Settings for SQL logging to the console
Configuration property namePurpose

hibernate.show_sql

Iftrue, log SQL directly to the console

hibernate.format_sql

Iftrue, log SQL in a multiline, indented format

hibernate.highlight_sql

Iftrue, log SQL with syntax highlighting via ANSI escape codes

When developing persistence logic with Hibernate, it’s very important to be able to see exactly what SQL is being executed.

2.2. The annotated entity Java class

The entity class in this tutorial isorg.hibernate.tutorial.annotations.Event.Observe that:

  • This class uses standard JavaBean naming conventions for property getter and setter methods, as well as private visibility for the fields.This is recommended, but it’s not a requirement.

  • The no-argument constructor, which is also a JavaBean convention,is a requirement for all persistent classes.Hibernate needs to instantiate objects for you, using Java Reflection.The constructor should have package-private orpublic visibility, to allow Hibernate to generate proxies and optimized code for field access.

TheEntity types section of the User Guide covers the complete set of requirements for the entity class.

We use annotations to identify the class as an entity, and to map it to the relational schema.

Identifying the class as an entity
@Entity(1)@Table(name="Events")(2)publicclassEvent{...}
1@jakarta.persistence.Entity marks theEvent class as an entity.
2@jakarta.persistence.Table explicitly specifies the name of the mapped table.Without this annotation, the table name would default toEvent.

Every entity class must have an identifier.

Identifying the identifier property
@Id(1)@GeneratedValue(2)privateLongid;
1@jakarta.persistence.Id marks the field as holding the identifier (primary key) of the entity.
2@jakarta.persistence.GeneratedValue specifies that this is asynthetic id, that is, a system-generated identifier (a surrogate primary key).

Other fields of the entity are considered persistent by default.

Mapping basic properties
privateStringtitle;@Column(name="eventDate")(1)privateLocalDateTimedate;
1@jakarta.persistence.Column explicitly specifies the name of a mapped column.Without this annotation, the column name would default todate, which is a keyword on some databases.

2.3. Example code

The classorg.hibernate.tutorial.annotations.HibernateIllustrationTest illustrates the use of the Hibernate’s native APIs, including:

  • Session andSessionFactory, and

  • org.hibernate.boot for configuration and bootstrap.

There are several different ways to configure and start Hibernate, and this is not even the most common approach.

The examples in these tutorials are presented as JUnit tests.A benefit of this approach is thatsetUp() andtearDown() roughly illustrate how aorg.hibernate.SessionFactory iscreated when the program starts, and closed when the program terminates.
Obtaining theSessionFactory
protectedvoidsetUp(){// A SessionFactory is set up once for an application!finalStandardServiceRegistryregistry=newStandardServiceRegistryBuilder().build();(1)(2)try{sessionFactory=newMetadataSources(registry)(3).addAnnotatedClass(Event.class)(4).buildMetadata()(5).buildSessionFactory();(6)}catch(Exceptione){// The registry would be destroyed by the SessionFactory, but we// had trouble building the SessionFactory so destroy it manually.StandardServiceRegistryBuilder.destroy(registry);}}
1ThesetUp() method first builds aStandardServiceRegistry instance which incorporates configuration information into a working set ofServices for use by theSessionFactory.
2Here we put all configuration information inhibernate.properties, so there’s not much interesting to see.
3Using theStandardServiceRegistry we create theMetadataSources which lets us tell Hibernate about our domain model.
4Here we have only one entity class to register.
5An instance ofMetadata represents a complete, partially-validated view of the application domain model.
6The final step in the bootstrap process is to build aSessionFactory for the configured services and validated domain model.TheSessionFactory is a thread-safe object that’s instantiated once to serve the entire application.

TheSessionFactory produces instances ofSession.Each session should be thought of as representing aunit of work.

Persisting entities
sessionFactory.inTransaction(session->{(1)session.persist(newEvent("Our very first event!",now()));(2)session.persist(newEvent("A follow up event",now()));});
1TheinTransaction() method creates a session and starts a new transaction.
2Here we create two newEvent objects and hands them over to Hibernate, calling thepersist() method to make these instances persistent.Hibernate is responsible for executing anINSERT statement for eachEvent.
Obtaining a list of entities
sessionFactory.inTransaction(session->{session.createSelectionQuery("from Event",Event.class)(1).getResultList()(2).forEach(event->out.println("Event ("+event.getDate()+") : "+event.getTitle()));});
1Here we use a very simpleHibernate Query Language (HQL) statement to load all existingEvent objects from the database.
2Hibernate generates and executes the appropriateSELECT statement, and then instantiates and populatesEvent objects with the data in the query result set.

2.4. Take it further!

Practice Exercises
  • Actually run this example to see the SQL executed by Hibernate displayed in the console.

  • Reconfigure the examples to connect to your own persistent relational database.

  • Add an association to theEvent entity to model a message thread.

3. Tutorial using JPA-standard APIs

Objectives
  • Configure Hibernate usingpersistence.xml

  • Bootstrap a Jakarta PersistenceEntityManagerFactory

  • Use annotations to provide mapping information

  • UseEntityManager to persist and query data

This tutorial is located within the download bundle underentitymanager/.

3.1. persistence.xml

JPA defines a different bootstrap process, along with a standard configuration file format namedpersistence.xml.In Java™ SE environments the persistence provider (Hibernate) is required to locate every JPA configuration file in the classpath at the pathMETA-INF/persistence.xml.

Configuration viapersistence.xml
<persistencexmlns="http://java.sun.com/xml/ns/persistence"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"version="2.0"><persistence-unitname="org.hibernate.tutorial.jpa">(1)<description>            Persistence unit for the Jakarta Persistence tutorial of the Hibernate Getting Started Guide</description><class>org.hibernate.tutorial.em.Event</class>(2)<properties>(3)<!-- Database connection settings --><propertyname="jakarta.persistence.jdbc.url"value="jdbc:h2:mem:db1;DB_CLOSE_DELAY=-1"/><propertyname="jakarta.persistence.jdbc.user"value="sa"/><propertyname="jakarta.persistence.jdbc.password"value=""/><!-- Automatically export the schema --><propertyname="jakarta.persistence.schema-generation.database.action"value="create"/><!-- Echo all executed SQL to console --><propertyname="hibernate.show_sql"value="true"/><propertyname="hibernate.format_sql"value="true"/><propertyname="hibernate.highlight_sql"value="true"/></properties></persistence-unit></persistence>
1Apersistence.xml file should provide a unique name for eachpersistence unit it declares.Applications use this name to reference the configuration when obtaining anEntityManagerFactory as we will see shortly.
2The<class/> element registers our annotated entity class.
3The settings specified as<properties/> elements were already discussed inConfiguration via properties file.Here JPA-standard property names are used where possible.

Configuration properties prefixed with the legacy Java EE namespacejavax.persistence are stillrecognized, but the Jakarta EE namespacejakarta.persistence should be preferred.

3.2. The annotated entity Java class

The entity class is exactly the same as inThe annotated entity Java class.

3.3. Example code

The previous tutorials used Hibernate native APIs.This tutorial uses the standard Jakarta Persistence APIs.

Obtaining the JPA EntityManagerFactory
protectedvoidsetUp(){entityManagerFactory=Persistence.createEntityManagerFactory("org.hibernate.tutorial.jpa");(1)}
1Notice again that the persistence unit name isorg.hibernate.tutorial.jpa, which matches the name from ourpersistence.xml.

The code to persist and query entities is almost identical toPersisting entities.Unfortunately,EntityManagerFactory doesn’t have a niceinTransaction() method likeSessionFactory does, so we had to write our own:

Managing transactions in JPA
voidinTransaction(Consumer<EntityManager>work){EntityManagerentityManager=entityManagerFactory.createEntityManager();EntityTransactiontransaction=entityManager.getTransaction();try{transaction.begin();work.accept(entityManager);transaction.commit();}catch(Exceptione){if(transaction.isActive()){transaction.rollback();}throwe;}finally{entityManager.close();}}
If you use JPA in Java SE, you’ll need to copy/paste this function into your project. Alternatively you could unwrap theEntityManagerFactory as aSessionFactory.

3.4. Take it further!

Practice Exercises
  • Learn how to use CDI to inject a container-managedEntityManager in Quarkus.Seethe Quarkus website for instructions.

4. Tutorial Using Envers

Objectives
  • Annotate an entity as historical

  • Configure Envers

  • Use the Envers APIs to view and analyze historical data

This tutorial is located within the download bundle underenvers/.

4.1. persistence.xml

This file is unchanged fromwhat we had before.

4.2. The annotated entity Java class

The entity class is also almost identical to what we hadpreviously.The major difference is the addition of the annotation@org.hibernate.envers.Audited, which tells Envers to automatically track changes to this entity.

4.3. Example code

The code saves some entities, makes a change to one of the entities and then uses the Envers API to pull back theinitial revision as well as the updated revision. A revision refers to a historical snapshot of an entity.

Using theorg.hibernate.envers.AuditReader
publicvoidtestBasicUsage(){...AuditReaderreader=AuditReaderFactory.get(entityManager);(1)EventfirstRevision=reader.find(Event.class,2L,1);(2)...EventsecondRevision=reader.find(Event.class,2L,2);(3)...}
1Anorg.hibernate.envers.AuditReader is obtained from theorg.hibernate.envers.AuditReaderFactory which wraps the JPAEntityManager.
2Thefind method retrieves specific revisions of the entity. The first call retrieves revision number 1 of theEvent with id 2.
3Later, the second call asks for revision number 2 of theEvent with id 2.

4.4. Take it further!

Practice Exercises
  • Provide a custom revision entity to additionally capture who made the changes.

  • Write a query to retrieve only historical data which meets some criteria. Use theUser Guide to see howEnvers queries are constructed.

  • Experiment with auditing entities which have various forms of relationships (many-to-one, many-to-many, etc). Tryretrieving historical versions (revisions) of such entities and navigating the object tree.

5. Credits

The full list of contributors to Hibernate ORM can be found on theGitHub repository.

The following contributors were involved in this documentation:

  • Steve Ebersole

Version 7.2.0.CR3
Last updated 2025-11-25 09:35:05 UTC

[8]ページ先頭

©2009-2025 Movatter.jp