Imagine having a tool that can automatically detect JPA and Hibernate performance issues.
Hypersistence Optimizer is that tool!
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 explain what we have changed in Hibernate 5.2.12.
Domain Model
Let’s assume we have the following JPA entity:
@Entity(name = "Book")
@Table(name = "book")
public class Book {
@Id
private Long id;
private String name;
@NaturalId
private long isbn;
//Getters and setters omitted for brevity
}
For more details about the @NaturalId annotation, check out this article.
And, we have the following Book entry in our database:
Book book = new Book();
book.setId(1L);
book.setName("High-Performance Java Persistence");
book.setIsbn(978_9730228236L);
entityManager.persist(book);
Writing JPA Criteria API queries is not very easy. The Codota IDE plugin can guide you on how to write such queries, therefore increasing your productivity.
For more details about how you can use Codota to speed up the process of writing Criteria API queries, check out this article.
Hibernate generates the following SQL query:
SELECT
b.id AS id1_0_,
b.isbn AS isbn2_0_,
b.name AS name3_0_
FROM
book b
WHERE
b.isbn = 9789730228236
As expected, the literal value was inlined in the generated SQL query.
SELECT
b.id AS id1_0_,
b.isbn AS isbn2_0_,
b.name AS name3_0_
FROM
book b
WHERE
b.name = ?
The literal is gone! Instead, we now got a PreparedStatement bind parameter.
Now, depending on the use case, you either want to use inlined literal values or substitute them with bind parameters. The advantage of using bind parameters is that the query Parse Tree and the Execution Plan (e.g. Oracle, SQL Server) could be cached.
However, sometimes caching the Execution Plan can cause more harm than good, especially if the literal values are skewed or there is a lot of contention on the Execution Plan Cache.
For this purpose, the HHH-9576 Jira issue was created.
Confguring the literal handling mode
Since Hibernate 5.2.12, you can use the LiteralHandlingMode to define the strategy used for handling literals in Criteria API queries. This enumeration takes three values:
AUTO, which works exactly as you’ve just seen. Numeric values are inlined while String-based ones are substituted with bind parameters.
INLINE, which will inline both numeric and String-based values.
BIND, which will substitute both numeric and String-based literals with bind parameters.
Specifying the expected Criteria API literal handling mode is actually a very nice enhancement. While the default AUTO mode might work just fine for many data access layers, in case you need to change the way literals are being handled, just provide the LiteralHandlingMode strategy you want to use, and Hibernate will switch to using that one instead.
2 Comments on “How does Hibernate handle JPA Criteria API literals”
Why not always use bind parameters? Shouldn’t query planners be able to figure skew out? If there is a lot of execution plan cache contention is it better to put lots of similar queries with different numbers into it?
Criteria API has different methods gor passing bind parameters. This topic is about literals that the devs wanted explicitly. That’s why, by default, those are not treated as bind parameters.
Why not always use bind parameters? Shouldn’t query planners be able to figure skew out? If there is a lot of execution plan cache contention is it better to put lots of similar queries with different numbers into it?
Criteria API has different methods gor passing bind parameters. This topic is about literals that the devs wanted explicitly. That’s why, by default, those are not treated as bind parameters.