The best way to use UNION, INTERSECT, and EXCEPT with Hibernate
Introduction In this article, we are going to see how we can write UNION, INTERSECT, and EXCEPT queries with Hibernate. While you could have always used all these relational set operations using native SQL queries, starting with Hibernate 6, you can use UNION, INTERSECT, and EXCEPT in JPQL and Criteria entity queries. This is all possible because of the new Semantic Query Model that Hibernate has introduced in version 6.
Hibernate SQM – Semantic Query Model
Introduction In this article, I’m going to explain what is the Hibernate SQM or Semantic Query Model so that you have a better picture of how JPQL or Criteria API queries are executed by Hibernate.
Hibernate 6 and JPQL Window Functions
Introduction In this article, I’m going to show you how you can use Hibernate 6 to write JPQL queries that use SQL Window Functions. This feature has been added in Hibernate 6, which provides a new query parser called SQM (Semantic Query Model), which is more powerful than the previous Hibernate HQL query capabilities.
Bulk Update and Delete with JPA and Hibernate
Introduction JPA and Hibernate allow us to execute bulk update and delete queries so that we can process multiple rows that match the business use case filtering criteria. When modifying multiple records, you have two options. You could either use batch processing or bulk processing. While batch processing is useful when entities are already managed by the current Persistence Context because it can reduce the number of INSERT, UPDATE, or DELETE statements that get executed, bulk processing allows us to modify the underlying database records with a single SQL statement.
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.
Multiline String literals with Java Text Blocks
Introduction In this article, show you how the new Java Text Blocks can help you increase the readability of JPQL, SQL queries, or JSON string values. Back in 2005, I was developing a .NET project and really liked the C# Verbatim String Literal feature since it allows you to write a multiline string value without having to concatenate each individual line. The Java 13 Text Blocks feature now offers the possibility of creating a multiline Java String values.
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.
The best way to use the JPQL DISTINCT keyword with JPA and Hibernate
Introduction In this article, we are going to how the JPQL DISTINCT keyword behaves depending on the underlying entity query type.
The best way to use SQL functions in JPQL or Criteria API queries with JPA and Hibernate
Introduction When executing an entity query (e.g. JPQL, HQL or Criteria API), you can use any SQL function without having to register it as long as the function is passed directly to the WHERE clause of the underlying SQL statement. However, if the SQL function is used in the SELECT clause, and Hibernate has not registered the SQL function (be it a database-specific or user-defined function), you will have to register the function prior to using it in an entity query. In this article, you are going to learn various ways to… Read More
A beginner’s guide to the Hibernate JPQL and Native Query Plan Cache
Introduction Every JPQL query must be compiled prior to being executed, and, because this process might be resource-intensive, Hibernate provides a QueryPlanCache for this purpose. For entity queries, the query String representation is parsed into an AST (Abstract Syntax Tree). For native queries, the parsing phase cannot compile the query, so it only extracts information about the named parameters and query return type.