How Do I Set Up Hibernate ORM 7 in a Maven Project?

In article 1, we introduced a Book entity and showed what Hibernate does when you persist an object. Now we will create a complete Maven application that saves that book to H2 and loads it again through Jakarta Persistence.

We will keep the same package, title, and manually assigned identifier. Author and Publisher relationships will come later in the roadmap; this article establishes the runnable project they will use.

1. Prepare Java and Maven

Install JDK 25 and Maven 3.9 or later. Check that Maven uses JDK 25:

java -version
mvn -version

This example uses Hibernate ORM 7.4.17.Final, released November 19, 2026, the latest stable release verified on November 23, 2026. It updates Week 01’s 7.4.15.Final within the same release series. The official compatibility matrix lists Java 25 and Jakarta Persistence 3.2. H2 2.5.252 is listed on the official H2 download page.

2. Create the Maven project

Create a directory named hibernate-book-catalog. Inside it, create the files below. Paths are relative to this directory.

File: pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>org.kodejava</groupId>
    <artifactId>hibernate-book-catalog</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.release>25</maven.compiler.release>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.hibernate.orm</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>7.4.17.Final</version>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <version>2.5.252</version>
            <scope>runtime</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.14.1</version>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>3.6.3</version>
                <configuration>
                    <mainClass>org.kodejava.hibernate.BookCatalogApp</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

Hibernate Core supplies the ORM implementation and brings in Jakarta Persistence transitively. H2 supplies the JDBC driver at runtime. The compiler plugin targets Java 25; the execution plugin runs our main class with the project’s dependencies.

3. Add the Book entity

File: src/main/java/org/kodejava/hibernate/Book.java

package org.kodejava.hibernate;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.Table;

@Entity
@Table(name = "books")
public class Book {
    @Id
    private Long id;

    @Column(nullable = false)
    private String title;

    protected Book() {
    }

    public Book(Long id, String title) {
        this.id = id;
        this.title = title;
    }

    public String getTitle() {
        return title;
    }
}

This is the entity introduced in article 1. Mapping annotations are on fields, so Hibernate accesses fields directly. A setter is not required for loading the title. The protected no-argument constructor allows the persistence provider to instantiate the entity. For now, the application assigns 1L as the identifier.

4. Configure the persistence unit

File: src/main/resources/META-INF/persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="https://jakarta.ee/xml/ns/persistence"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="https://jakarta.ee/xml/ns/persistence https://jakarta.ee/xml/ns/persistence/persistence_3_2.xsd"
             version="3.2">
    <persistence-unit name="book-catalog" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
        <class>org.kodejava.hibernate.Book</class>
        <exclude-unlisted-classes>true</exclude-unlisted-classes>
        <properties>
            <property name="jakarta.persistence.jdbc.driver" value="org.h2.Driver"/>
            <property name="jakarta.persistence.jdbc.url" value="jdbc:h2:mem:book_catalog"/>
            <property name="jakarta.persistence.jdbc.user" value="sa"/>
            <property name="jakarta.persistence.jdbc.password" value=""/>
            <property name="jakarta.persistence.schema-generation.database.action" value="drop-and-create"/>
            <property name="hibernate.show_sql" value="true"/>
            <property name="hibernate.format_sql" value="true"/>
        </properties>
    </persistence-unit>
</persistence>

A persistence unit groups entity mappings and connection settings under one name. We explicitly register Book and disable discovery of unlisted entities so that the model is easy to follow.

RESOURCE_LOCAL means our application controls transactions through EntityTransaction. The H2 URL creates a named database in memory. Hibernate’s connection pool keeps connections available during the application; the database disappears when its last connection closes. Nothing is saved to a database file.

drop-and-create recreates the mapped schema when the persistence unit starts. Use it only for this disposable example: it can delete existing tables and data. Later, we will manage persistent schema changes with migrations. Hibernate detects the H2 SQL dialect from JDBC metadata, so no explicit dialect setting is needed.

The bootstrap and configuration approach follows Hibernate’s Jakarta Persistence quickstart.

5. Save and load a book

File: src/main/java/org/kodejava/hibernate/BookCatalogApp.java

package org.kodejava.hibernate;

import jakarta.persistence.EntityManager;
import jakarta.persistence.EntityManagerFactory;
import jakarta.persistence.EntityTransaction;
import jakarta.persistence.Persistence;

public class BookCatalogApp {
    public static void main(String[] args) {
        try (EntityManagerFactory factory =
                     Persistence.createEntityManagerFactory("book-catalog")) {
            saveBook(factory);
            loadBook(factory);
        }
    }

    private static void saveBook(EntityManagerFactory factory) {
        try (EntityManager entityManager = factory.createEntityManager()) {
            EntityTransaction transaction = entityManager.getTransaction();
            try {
                transaction.begin();
                entityManager.persist(new Book(1L, "Effective Java"));
                transaction.commit();
                System.out.println("Saved book: Effective Java");
            } catch (RuntimeException exception) {
                if (transaction.isActive()) {
                    transaction.rollback();
                }
                throw exception;
            }
        }
    }

    private static void loadBook(EntityManagerFactory factory) {
        try (EntityManager entityManager = factory.createEntityManager()) {
            Book book = entityManager.find(Book.class, 1L);
            if (book == null) {
                throw new IllegalStateException("Book 1 was not found");
            }
            System.out.println("Loaded book: " + book.getTitle());
        }
    }
}

Persistence.createEntityManagerFactory() finds META-INF/persistence.xml on the classpath and selects the unit named book-catalog. Create one factory for the application and close it at shutdown. Each EntityManager represents a separate persistence context and must also be closed. Jakarta Persistence 3.2 supports try-with-resources for both.

The write runs inside a transaction. Committing flushes the insert to H2. If the operation fails and the transaction is still active, we roll it back and propagate the failure.

The read uses a new entity manager, so it does not reuse the original persistence context’s managed book. A primary-key find() outside a transaction is permitted here because we are simply reading without a lock. In larger applications, transaction boundaries should follow the complete unit of work.

6. Run the application

From the directory containing pom.xml, run:

mvn clean compile exec:java

Maven downloads dependencies, compiles both Java files, copies resources into target/classes, and runs BookCatalogApp.

Expected application output:

Saved book: Effective Java
Loaded book: Effective Java

Hibernate also prints startup messages and SQL. Formatting may vary, but the SQL includes table creation, an insert, and a select:

create table books (
    id bigint not null,
    title varchar(255) not null,
    primary key (id)
)
insert into books (title, id) values (?, ?)
select b1_0.id, b1_0.title from books b1_0 where b1_0.id=?

Question marks are JDBC bind parameters, not missing values. Running the command again starts with a fresh in-memory database, so the assigned ID does not collide with a previous run.

If startup reports that no persistence provider exists for book-catalog, check the exact filename, the META-INF directory, the persistence-unit name, and the Hibernate dependency. If compilation reports that release 25 is unsupported, check the JDK shown by mvn -version. A warning about Hibernate’s built-in connection pool is expected for this small tutorial; production applications should configure a production connection pool.

Maven Dependencies

These are all direct library dependencies needed by this example. The complete POM above also pins the build plugins. Jakarta Persistence is supplied transitively by Hibernate Core; no separate driver installation or H2 server is required.

<dependencies>
    <dependency>
        <groupId>org.hibernate.orm</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>7.4.17.Final</version>
    </dependency>
    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <version>2.5.252</version>
        <scope>runtime</scope>
    </dependency>
</dependencies>

Leave a Comment.

This site uses Akismet to reduce spam. Learn how your comment data is processed.