Skip to content

Sample spring boot application that demonstrates how to map a java enum as a JPA entity using hibernate.

Notifications You must be signed in to change notification settings

djarnis73/jpa-table-backed-enum

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Tags

java, jpa, hibernate, spring-boot

Background

I was working on a project where we were inserting lots of records in a database and I was looking for a way to persist enums efficiently.

The obvious solution is to use @Enumerated(EnumType.ORDINAL), but I also wanted a way to translate the ordinal into the enum name.

This can of course be handled manually by creating a reference table etc. but I though it would be nice if the JPA layer could help create and populate the table.

Enums as Entities

The JPA Spec (JSR 338, section 2.1) states that:

The entity class must have a no-arg constructor. The entity class may have other constructors as well.The no-arg constructor must be public or protected.

The entity class must be a top-level class. An enum or interface must not be designated as an entity.

The entity class must not be final. No methods or persistent instance variables of the entity class may be final.

However Hibernate documentation states that

Hibernate, however, is not as strict in its requirements

So after some experimentation I managed to define an enum as an entity.

One of the key points is the EnumAsEntityInterceptor, which deals with instantiation, since an enum does not have a constructor.

About

Sample spring boot application that demonstrates how to map a java enum as a JPA entity using hibernate.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages