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 register SQL functions with JPA and Hibernate.

Continue reading “The best way to use SQL functions in JPQL or Criteria API queries with JPA and Hibernate”

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.

Continue reading “A beginner’s guide to the Hibernate JPQL and Native Query Plan Cache”

How do find and getReference EntityManager methods work when using JPA and Hibernate

Introduction

While doing my High-Performance Java Persistence training, I realized that not all developers are familiar with the getReference method of the JPA EntityManager and most of them use find almost exclusively.

In this article, we are going to see the difference between the find and getReference method so that it’s clear when to apply them depending on the underlying use case.

Continue reading “How do find and getReference EntityManager methods work when using JPA and Hibernate”

How to synchronize bidirectional entity associations with JPA and Hibernate

Introduction

While answering this StackOverflow question, I realized that it’s a good idea to summarize how various bidirectional associations should be synchronized when using JPA and Hibernate.

Therefore, in this article, you are going to learn how and also why you should always synchronize both sides of an entity relationship, no matter if it’s @OneToMany, @OneToOne or @ManyToMany.

Continue reading “How to synchronize bidirectional entity associations with JPA and Hibernate”

How to use @PrePersist and @PreUpdate on Embeddable with JPA and Hibernate

Introduction

In a previous article, I explained how you could audit entity modifications using the JPA @EntityListeners for embeddable types.

Since Hibernate ORM 5.2.17 now allows you to use the @PrePersist and @PreUpdate JPA entity listeners, we can simplify the previous example, as you will see in this article.

Continue reading “How to use @PrePersist and @PreUpdate on Embeddable with JPA and Hibernate”