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.

Free ebook: Java 8 support in Hibernate 5

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.

Free ebook: Native Queries with Hibernate

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.

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:

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.

Hibernate Search

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.