Vlad Mihalcea

How to bind custom Hibernate parameter types to JPA queries

Imagine having a tool that can automatically detect if you are using JPA and Hibernate properly. Hypersistence Optimizer is that tool!

Introduction

In this article, we are going to see how we can bind a custom Hibernate parameter type to a JPA query.

I decided to write this article after one of my hibernate-types framework users created this very interesting issue on GitHub.

Read More

How to intercept entity changes with Hibernate event listeners

Imagine having a tool that can automatically detect if you are using JPA and Hibernate properly. Hypersistence Optimizer is that tool!

Introduction

In this article, we are going to see how the Hibernate event listeners work and how you add your custom listeners to intercept entity changes and replicate them to other database tables.

Recently, one of my blog readers asked a very good question on StackOverflow.

Since my main goal as a Hibernate Developer Advocate is to help Java developers get the most out of JPA and Hibernate, I decided that this is a good opportunity to talk about the Hibernate event listener mechanism.

Read More

The best way to initialize LAZY entity and collection proxies with JPA and Hibernate

Imagine having a tool that can automatically detect if you are using JPA and Hibernate properly. Hypersistence Optimizer is that tool!

Introduction

In this article, we are going to see the best way to initialize LAZY proxies and collections when using JPA and Hibernate.

I decided to write this article because there are way too many resources available on the Internet that mislead the reader into using awkward and inefficient practices.

Read More

Three years as a Hibernate Developer Advocate

Imagine having a tool that can automatically detect if you are using JPA and Hibernate properly. Hypersistence Optimizer is that tool!

Introduction

Three years ago, I started working as a Developer Advocate for the Hibernate team at Red Hat.

Each year, I write a new report to summarize the accomplishments for the Hibernate project and its community. If you’re curious about the previous year reports, check out the

Read More

How to execute SQL functions with multiple parameters in a JPQL query with Hibernate

Imagine having a tool that can automatically detect if you are using JPA and Hibernate properly. Hypersistence Optimizer is that tool!

Introduction

In this article, we are going to see how you can execute SQL functions with multiple parameters in JPQL queries with Hibernate. I recently stumbled on this requirement while answering this StackOverflow question which is a never-ending source of inspiration for articles on my blog.

Read More

The best way to use the JPQL DISTINCT keyword with JPA and Hibernate

Imagine having a tool that can automatically detect if you are using JPA and Hibernate properly. Hypersistence Optimizer is that tool!

Introduction

In this article, we are going to how the JPQL DISTINCT keyword behaves depending on the underlying entity query type.

Read More

How to map camelCase properties to snake_case column names with Hibernate

Imagine having a tool that can automatically detect if you are using JPA and Hibernate properly. Hypersistence Optimizer is that tool!

Introduction

In this article, you are going to learn how to map cameCase entity properties (e.g. phoneNumber) to snake_case column names (e.g. phone_number) using a Hibernate naming strategy.

While you could achieve the same goal with the name attribute of JPA @Column annotation, it’s much more convenient to use a custom Hibernate strategy to apply this naming convention consistently.

Read More

How to optimize JPQL and Criteria API query plans with Hibernate Statistics

Imagine having a tool that can automatically detect if you are using JPA and Hibernate properly. Hypersistence Optimizer is that tool!

Introduction

Every entity query, be it JPQL or Criteria API, needs to be parsed and compiled to an AST (Abstract Syntax Tree) in order to generate the associated SQL query. The entity query compilation takes time, as explained in this article so Hibernate provides a QueryPlanCache to store already-compiled plans.

Starting with Hibernate 5.4, the Hibernate Statistics mechanism allows you to monitor the Query Plan Cache and this article will show you how to take advantage of this feature to speed up IN query performance.

For an introduction about the Hibernate Statistics mechanism, check out this article.

Read More

How to merge entity collections with JPA and Hibernate

Imagine having a tool that can automatically detect if you are using JPA and Hibernate properly. Hypersistence Optimizer is that tool!

Introduction

In this article, you are going to learn why overwriting entity collections is an anti-pattern and how you can merge collections both effectively and efficiently with JPA and Hibernate.

The reason I decided to write this article is that I’ve been this question asked over and over and over again.

Read More

How to map java.time.Year and java.time.Month with JPA and Hibernate

Imagine having a tool that can automatically detect if you are using JPA and Hibernate properly. Hypersistence Optimizer is that tool!

Introduction

In this article, we are going to see how you can map the java.time.Year and java.time.Month with both JPA and Hibernate.

As explained in this article, JPA 2.2 supports LocalDate, LocalTime, LocalDateTime, OffsetTime, OffsetDateTime from the java.time package. Hibernate has been supporting the Java 8 Date/Time classes since 5.0 via the hibernate-java8 dependency, but since version 5.2, this dependency was merged with hibernate-core so you get the Duration, Instant and ZonedDateTime types in addition to the ones supported by JPA 2.2.

However, neither JPA nor Hibernate supports the java.time.Year type out-of-the-box. As you will see, adding support for java.time.Year is very easy with both standard JPA or Hibernate.

Read More