How to order entity subclasses by their class type using JPA and Hibernate

Introduction

One of my blog readers asked me a very good question, and so I decided to turn the answer it into a blog post.

In this article, we are going to see how to order entity subclasses when executing a JPQL query with Hibernate.

Continue reading “How to order entity subclasses by their class type using JPA and Hibernate”

Advertisements

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”

How to emulate @CreatedBy and @LastModifiedBy from Spring Data using the @GeneratorType Hibernate annotation

Introduction

Hibernate comes with many additions to the standard JPA specification. One such example is the @GeneratorType annotation which allows you to customize the way a given entity property value is automatically generated.

If you’re using Spring Data, you can simply use the @CreatedBy and @LastModifiedBy annotations and the annotated entity properties are going to be populated with the currently logged user.

If you’re not using Spring Data, then you can easily emulate the same behavior using the Hibernate-specific @GeneratorType annotation and the ValueGenerator callback mechanism.

Continue reading “How to emulate @CreatedBy and @LastModifiedBy from Spring Data using the @GeneratorType Hibernate annotation”

How to inherit properties from a base class entity using @MappedSuperclass with JPA and Hibernate

Introduction

Last week, one of my blog readers asked me of a way to reuse the @Id mapping so that it won’t have to be declared on each an every entity.

Because this is a good opportunity to introduce @MappedSuperclass, I decided to answer the question with a blog post.

Continue reading “How to inherit properties from a base class entity using @MappedSuperclass with JPA and Hibernate”