How to get the SQL query from JPQL or JPA Criteria

Introduction In this article, I’m going to show you how you can get the auto-generated SQL query from a JPQL or JPA Criteria API entity query.

JPA Criteria Metamodel Generation and Usage Guide

Introduction In this article, I’m going to show you how to generate the JPA Metamodel using Hibernate, and how you can use it to write typesafe Criteria API queries.

Bulk update optimistic locking with JPA and Hibernate

Introduction In this article, we are going to see how we can adjust a bulk update statement so that it takes optimistic locking into consideration. While Hibernate has been supporting versioned HQL queries for a very long time, it’s actually very easy to achieve this goal even with standard JPQL or JPA Criteria API.

How to write JPA Criteria API queries using Codota

Introduction Writing JPA Criteria API queries is not very easy. The Codota IDE plugin can guide you on how to write such queries, which greatly simplifies the process. In this article, we are going to see how Codota works and how it can help you discover a given API.

How to query by entity type using JPA Criteria API

Introduction Inspired by this Hibernate forum post, I decided to write an article to explain how you can filter by the entity type using Criteria API.

How to map an immutable entity with JPA and Hibernate

Introduction In this article, you are going to find out how to map an immutable entity when using JPA and Hibernate. If your Domain Model requires that a given entity should not be modified by the data access logic, Hibernate can enforce this requirement if the entity is marked with the @Immutable annotation.

JPA Criteria API Bulk Update and Delete

Introduction JPA Criteria API bulk update delete is a great feature that allows you do build bulk update and delete queries using the JPA 2.1 Criteria API support via CriteriaUpdate and CriteriaDelete. Because one of our community members asked me on the Hibernate forum about this topic, I decided it is a good opportunity to write about this lesser-known JPA Criteria API feature.

How does Hibernate handle JPA Criteria API literals

Introduction The JPA specification is like a Java interface, However, when it comes to performance, implementation details matter a lot. That’s why, even if you use the JPA standard, you still need to know how the underlying provider implements the standard specification. For instance, if we take this tweet from Gareth Western: We can clearly see that there is an issue in the way literals might be handled by Hibernate when executing a Criteria API query. Therefore, in this article, we are going to see how literals are handled by Hibernate and… Read More

EAGER fetching is a code smell when using JPA and Hibernate

Introduction Hibernate fetching strategies can really make a difference between an application that barely crawls and a highly responsive one. In this post, I’ll explain why you should prefer query-based fetching instead of global fetch plans. Fetching 101 Hibernate defines four association retrieving strategies: Fetching Strategy Description Join The association is OUTER JOINED in the original SELECT statement Select An additional SELECT statement is used to retrieve the associated entity(entities) Subselect An additional SELECT statement is used to retrieve the whole associated collection. This mode is meant for to-many associations Batch An… Read More

Why you should always check the SQL statements generated by Criteria API

Introduction Criteria API is very useful for dynamically building queries, but that’s the only use case where I’d use it. Whenever you have a UI with N filters that may arrive in any M combinations, it makes sense to have an API to construct queries dynamically, since concatenating strings is always a path I’m running away from. The question is, are you aware of the SQL queries your Criteria API generates behind the scenes? I’ve been reviewing many such queries lately, and I’ve been struck by how easy it is to get… Read More