Avoid repeating attributes in JPA (Java Persistence API) entities

Last Updated:  June 16, 2022 | Published: June 17, 2018

Today I want to show you a way to avoid repeating attributes in your JPA entities. Let's consider some of your columns e.g. the creation date, an intern identifier are present on most of your entities. In most of the timestamp column cases, you'll use the JPA lifecycle listeners for @PreUpdate or @PrePersist. Repeating these columns over and over again could lead to developer errors and you need to test them on every entity.

To solve this problem, JPA offers an annotation called @MappedSuperclass and make use of Java's inheritance and extract all repeating attributes to this superclass. This superclass won't be mapped to a table in the database.

In the following example, I'll model a Product and a Customer domain entity which have some columns in common. In addition, I make use of some Hibernate specific annotations like @CreationTimestamp/@UpdateTimestamp which are not in the JPA standard right now, but the result of these annotations could also be achieved with @PreUpdate/@PrePersist.

Due to the fact that all the JPA entities need a primary ID, I also extracted the ID column to my superclass:

The domain JPA entities look like the following:

With this solution, you can control your common columns in one place. It also improves the testability as you just need to verify these columns on one entity. One important downside of this solution is that you can only use the JPA lifecycle listener annotations @PrePersist/@PreUpdate ... once and not multiple times.

I created an example project for this blog post with an embedded H2 in-memory. You can find the sources on GitHub and try them on your machine.

Find more persistence-related tutorials here.

See you!

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}