How to map JSON collections using JPA and Hibernate

Introduction

The open-source hibernate-types project allows you to map Java objects or Jackson JsonNode as JPA or Hibernate entity properties, and, thanks to our awesome contributors, we have added support for storing type-safe JSON collections.

In this article, you are going to see how to achieve this goal.

Continue reading “How to map JSON collections using JPA and Hibernate”

How to bootstrap JPA programmatically without the persistence.xml configuration file

Introduction

One of my Twitter followers asked me if there is any documentation for bootstrapping JPA programmatically so that we can replace the persistence.xml configuration file with a Java configuration:

Previously, I wrote an article about bootstrapping Hibernate without the persistence.xml, but that solution was based on the Hibernate-specific EntityManagerFactoryBuilderImpl.

In this article, I’m going to give you a solution that works with any JPA provider since it’s based on standard Java Persistence API.

Continue reading “How to bootstrap JPA programmatically without the persistence.xml configuration file”

How to map a JPA @ManyToOne relationship to a SQL query using the Hibernate @JoinFormula annotation

Introduction

Someone asked me to answer the following StackOverflow question, and, because the question is very interesting from an SQL perspective, I decided to turn the answer it into a blog post.

In this article, we are going to see how to map a JPA @ManyToOne association to the result of a SQL query using the Hibernate-specific @JoinFormula annotation.

Continue reading “How to map a JPA @ManyToOne relationship to a SQL query using the Hibernate @JoinFormula annotation”

How to audit entity modifications using the JPA @EntityListeners, @Embedded, and @Embeddable annotations

Introduction

After I wrote the article about inheriting properties from a base class entity using @MappedSuperclass, I got an avalanche of opinions, but this one from Lukas deserves a blog post:

While @MappedSuperclass has its benefit, allowing you to reuse even the @Id mapping, as well as being more lenient towards Hibernate-specific auto-generated properties like @GeneratedValue, using Embeddable types is the other JPA alternative for reusing a bunch of properties among multiple entities.

In this article, we are going to see how we can reuse several audit-related properties using @Embeddable and another awesome JPA feature, @EntityListeners.

Continue reading “How to audit entity modifications using the JPA @EntityListeners, @Embedded, and @Embeddable annotations”