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:
- Most Popular
- JPA 2.2
- Hibernate 5
- Performance
- JPA 2.1
- Native SQL Queries
- Mapping Definitions
- Create and Update Database Schemata
- Hibernate Envers
- Hibernate Search
Most Popular
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:
- Hibernate Best Practices
- 7 Tips to boost your Hibernate performance
- Ultimate Guide to JPQL Queries with JPA and Hibernate
- Native Queries – How to call native SQL queries with JPA
- Hibernate Logging Guide – Use the right config for development and production
- 11 JPA and Hibernate query hints every developer should know
- JPA 2.1 – 12 features every developer should know
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.
- How To Map The Date And Time API with JPA 2.2
- JPA 2.2 Introduces @Repeatable Annotations
- JPA 2.2’s new getResultStream() method and how you should NOT use it
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 interesting changes and additions to the existing APIs. The development team not only introduced the support for Java 8, 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
- Benefits of @Repeatable annotations in Hibernate 5.2
- How to get query results as a Stream with Hibernate 5.2
- How to persist LocalDateTime & Co with Hibernate
- How to use Java 8’s Optional with Hibernate
Improved Support for Common Use Cases
- How to persist creation and update timestamps with Hibernate
- How to fetch multiple entities by their primary key
- How to join unrelated entities with JPA and 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.
- 7 Tips to Boost your Hibernate Performance
- Free Mini Course: How to find and fix n+1 select issues with Hibernate
- 5 Tips to Write Efficient Queries with JPA and Hibernate
- 5 Ways to Initialize Lazy Relations and When to Use Them
- How to Activate Hibernate Statistics to Analyze Performance Issues
- How to Perform a Bulk Update with a Native SQL query
- Criteria Update/Delete – The Easy Way to Implement Bulk Operations with JPA 2.1
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
- JPA 2.1 – How to implement an Attribute Converter
- How to persist LocalDate and LocalDateTime with JPA
- JPA 2.1 Attribute Converter – The better way to persist enums
- How to use a JPA Attribute Converter to encrypt your data
Entity Graphs
- JPA 2.1 Entity Graph – Part 1: Named entity graphs
- JPA 2.1 Entity Graph – Part 2: Define lazy/eager loading at runtime
Stored Procedure Calls
- How to Call a Stored Procedure with a @NamedStoredProcedureQuery
- How to Define a Stored Procedure Call at Runtime
Criteria API
Constructor Result Mappings
Generating DB Schema
Create Named Queries At Runtime
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.
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 efficiently retrieve the required information. In these situations, native SQL queries are most often the best solution. You can use them in a similar way as JPQL queries and they allow you to use all SQL features supported by your database.
- How to call native SQL queries with JPA and Hibernate
- How to perform bulk update with native SQL queries
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:
- Result Set Mapping: The Basics
- Result Set Mapping: Complex Mappings
- Result Set Mapping: Constructor Result Mappings
- Result Set Mapping: Hibernate specific features
Mapping Definitions
Hibernate and JPA support lots of different mapping annotations that allow you to map complex domain and table models.
- 5 Reasons to Use JPA and Hibernate
- Should you use JPA for your next Project?
- How to Generate Primary Keys with JPA and Hibernate
- How to Generate UUIDs as Primary Keys with Hibernate
- @NaturalId – A good way to Persist Natural IDs with Hibernate?
- How to map a java.util.Date to a Database Column
- How to persist LocalDateTime & Co with Hibernate
- How to persist LocalDate and LocalDateTime with JPA
- How to map an Enum to a Database Column
- JPA 2.1 Attribute Converter – The better way to persist enums
- How to map Bidirectional many-to-one Associations
- How to map a Bidirectional many-to-many Association
- Introduction to JPA FetchTypes
- How to map an Entity Attribute to an Optional
Create and Update Database Schemata
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 generate your first database scripts and can be a good option to set up test systems.
- Should you Create or Generate your Table Model?
- Standardizes Schema Generation and Data Loading with JPA 2.1
Version-based database migration
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:
- Version-Based Database Migration with Liquibase – Getting Started
- Version-Based Database Migration with Liquibase – Update an Existing Database
- How To Implement Automatic Database Updates By Integrating Liquibase
Flyway is another tool to implement version-based database migrations:
- Getting Started with Flyway and Version-Based Database Migration
- How to Use Java-Based Migrations and Callbacks to Implement Complex Database Migrations With Flyway
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 Envers – Getting Started
- Hibernate Envers – Extract Data from your Audit Log with the AuditReader
- Hibernate Envers – Extend the Standard Revision to Store Additional User Information
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 questions 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.