How to intercept entity changes with Hibernate event listeners

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.

Continue reading “How to intercept entity changes with Hibernate event listeners”

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

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.

Continue reading “The best way to initialize LAZY entity and collection proxies with JPA and Hibernate”

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

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.

Continue reading “How to execute SQL functions with multiple parameters in a JPQL query with Hibernate”

How to merge entity collections with JPA and Hibernate

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.

Continue reading “How to merge entity collections with JPA and Hibernate”

How to map the Java YearMonth type with JPA and Hibernate

Introduction

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

As I explained in this article, JPA 2.2 supports the following Date/Time types introduced in Java 8:

  • java.time.LocalDate
  • java.time.LocalTime
  • java.time.LocalDateTime
  • java.time.OffsetTime
  • java.time.OffsetDateTime

Apart from supporting those, Hibernate supports also:

  • java.time.Duration
  • java.time.Instant
  • java.time.ZonedDateTime

However, neither JPA nor Hibernate support the java.time.YearMonth out-of-the-box. As you will see, adding support for java.time.YearMonth is really straightforward for both standard JPA or Hibernate.

Continue reading “How to map the Java YearMonth type with JPA and Hibernate”