Tutorials

Persistence with JPA and Hibernate is one of the main topics in my daily work and on this blog. This page provides a curated list of some of the best and most popular posts in the categories:

 

I covered a broad range of Hibernate and JPA topics, from beginner tutorials about writing JPQL queries, over Hibernate best practices to advanced topics, like query hints.
Here are some of the most popular posts:

 

JPA 2.2

JPA 2.2 was just a small maintenance release, but it brought a few interesting changes. For most developers, the added support for Java’s Date and Time API and new getResultStream() method were probably the most important changes.

 

Native Queries with Hibernate - 3d cover

Free ebook: Java 8 support in Hibernate 5

Join the free Thoughts on Java Library to get access to lots of member-only content, like the “Java 8 support in Hibernate 5” ebook.

Hibernate 5

Hibernate 5 brought a lot of changes and additions to the existing APIs. The development team not only introduced the support for Java 8, but they also extended the existing APIs to make the implementation of common use cases easier.
I listed the 5 most important changes in 5 new features in Hibernate 5 every developer should know.

Java 8 Support

Improved Support for Common Use Cases

 

JPA 2.1

JPA 2.1 introduced a set of new features to the specification to make the work with a relational database easier and more efficient. You can get an overview of all the new features in JPA 2.1 – 12 features every developer should know.

Or if you already know what you are searching for, have a look at the following list of JPA 2.1 related articles.

Attribute Converter

Entity Graphs

Stored Procedure Calls

Custom Database Functions

Criteria API

Constructor Result Mappings

Generating DB Schema

Create Named Queries At Runtime

 

Native Queries with Hibernate - 3d cover

Free ebook: Native Queries with Hibernate

Join the free Thoughts on Java Library to get access to lots of member-only content, like the “Native Queries with Hibernate” ebook.

Performance

Implementing your persistence tier with Hibernate is quite easy and as the most popular JPA implementation also the way to go for Java EE applications. But what starts as nice and easy often turns out to be an issue as soon as the requirements get more challenging. You need to know more than just the basics of Hibernate and JPA to create an application that performs well on a huge database or under high load.

 

General Concepts

If you’re new to JPA and Hibernate, you might want to start with some basic articles that explain the general concepts and show you when JPA and Hibernate are a good fit for your project.

 

Entity Mappings

Hibernate and JPA support lots of different mapping annotations that allow you to map complex domain and table models.

General

Identifier Mappings

Attribute Mappings

Association Mappings

Custom Data Types

 

Configuration

JPA and Hibernate use a huge set of defaults so that you need almost no configuration. The only configuration you need to provide are a few parameters in the persistence.xml file and a logging configuration. You can learn more about them here:

 

Native SQL Queries

The Java Persistence Query Language (JPQL) is the most common way to query data from a database with JPA. But it supports only a small subset of the SQL standard, and it also provides no support for database specific features.

If queries get complex, these limitations become an issue, and you need to take a different approach to retrieve the required information efficiently. In these situations, native SQL queries are most often the best solution. You can similarly use them as JPQL queries, and they allow you to use all SQL features supported by your database.

Result Mappings

One of the downsides of native SQL queries is, that they return a List<Object[]> and that you need to cast all these Objects into the right type. An easy solution for it are Result Set Mappings which I explained in a series of blog posts:

Building Native Queries With jOOQ

jOOQ provides a powerful DSL that enables you to build fully-featured SQL queries. If you want to use it together with Hibernate, you can build your query with jOOQ and execute it within your current persistence context as a native SQL query.

 

Create and Update Database Schemata

If you ever shipped an application to hundreds of customers or updated a production database without causing downtime, you know that it isn’t as easy as it might seem. A good migration process requires much more effort, and skills than a lot of developers expect. Here are a few articles that might be helpful.

Schema generation with JPA and Hibernate

You can use Hibernate and JPA to generate your database schema based on entity mappings or SQL scripts. I don’t recommend to use this approach for production, but it helps to create your first database scripts and can be a good option to set up test systems.

Version-based database migration tools

If you need to update the database of your production system, you should prefer a version-based migration approach. The general idea is simple: You apply the same versioning approach for your source code and database.

There are several tools available that help you implement a version-based database migration. One of them is Liquibase:

Flyway is another tool to implement version-based database migrations:

Updating a schema without causing downtime

Updating a schema while your application is offline is quite simple. You just perform all required operations, restart the application and hope that everything works fine.

But that’s no longer the case if your customers don’t accept any downtime. You then need a complex, multi-step migration process that enables the old and the new version of your application to use the same database. I show you how to do that in Update your Database Schema Without Downtime.

 

Hibernate Envers

A lot of business applications require an audit log that documents all changes that were performed on the managed data. There are lots of different options to implement such a log. One of them is Hibernate Envers. It just takes a few annotations to document all changes in the audit tables, and Envers also provides a powerful API for extracting information from your audit log.

 

Full-text search has become a common requirement for modern enterprise applications, and there are several good implementations available that provide powerful indexing and search capabilities. But one important question remains when you decide to add one of them to your application: How do you keep the full-text search indexes in sync with of your database?

Hibernate Search provides an easier solution. It integrates with Hibernate ORM, updates the Lucene and Elasticsearch indexes transparently and provides a query DSL for full-text queries.