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

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.

Continue reading “How to map java.time.Year and java.time.Month 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”

Five years of blogging

Persistence, right?

It’s been five years since I started this blog, and every year I happen to write a report. If you are curious about my progress, check out the previous reports:

Page Views

Compared to last year, the traffic almost doubled, and now I get around 270K page views and over 130K visitors on a monthly basis.

Overall, in these five years, I got over 5 million page views and 2.7 million visitors. Cool, right?

Continue reading “Five years of blogging”

Spring read-only transaction Hibernate optimization

Introduction

In this article, I’m going to explain how the Spring read-only transaction Hibernate optimization works.

After taking a look at what the Spring framework does when enabling the readOnly attribute on the @Transactional annotation, I realized that only the Hibernate flush mode is set to FlushType.MANUAL without propagating the read-only flag further to the Hibernate Session.

So, in the true spirit of open-source software developer, I decided it’s time to make a change.

Continue reading “Spring read-only transaction Hibernate optimization”