How to Map an Enum with Specific Integer Values Using JPA

Question

How can I map an enum in JPA to use specific integer values for each enum entry while saving only these integer values?

@Entity
@Table(name = "AUTHORITY_")
public class Authority implements Serializable {

  public enum Right {
      READ(100), WRITE(200), EDITOR (300);

      private int value;

      Right(int value) { this.value = value; }

      public int getValue() { return value; }
  };

  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  @Column(name = "AUTHORITY_ID")
  private Long id;

  private Right right;
}

Answer

Mapping enums in JPA to specific integer values requires custom handling, as JPA's default behavior is to use ordinal values (0, 1, 2). This guide outlines several solutions to achieve the desired mapping of enums with fixed integer values.

@Basic
private int intValueForAnEnum;

@PrePersist
void populateDBFields() {
  intValueForAnEnum = right.getValue();
}

@PostLoad
void populateTransientFields() {
  right = Right.valueOf(intValueForAnEnum);
}

Causes

  • JPA by default maps enums using ordinal values, which do not correspond to the custom integers specified in the enum definition.
  • Complexity increases with the need to maintain enum values in the database that do not reflect their ordinal indexes.

Solutions

  • **Using @Enumerated with a custom int field**: One straightforward approach is to create an additional integer field to store the values derived from the `enum`. Then, utilize `@PrePersist` and `@PostLoad` lifecycle methods to convert between the enum and its integer representation.
  • **Implementing a custom converter**: With the use of a custom converter, you can define how to convert between the enum and the desired int representation. This is more efficient and keeps your entity cleaner.

Common Mistakes

Mistake: Using `@Enumerated(EnumType.ORDINAL)` without understanding it maps to ordinal values.

Solution: Instead, use an integer field and manage conversions manually.

Mistake: Overcomplicating the enum mapping with too many conversions.

Solution: Utilize JPA lifecycle methods for simpler mapping between enum and persistence.

Helpers

  • JPA enum mapping
  • JPA fixed values enum
  • JPA enum integer values
  • Java persistence API enum
  • Spring JPA enum conversion
  • Hibernate enum mapping

Related Questions

⦿How to Rename a File in Java: Handling Existing Files

Learn how to rename files in Java handling conflicts and appending content to existing files like test1.txt.

⦿How to Ignore a Specific FindBugs Warning in Your Code?

Learn how to ignore a specific FindBugs warning using annotations and strategies for cleaner code reviews.

⦿How to Use Mockito with JUnit 5 for Dependency Injection

Learn how to integrate Mockito with JUnit 5 for effective dependency injection in your tests. Discover best practices and code examples.

⦿Should Javadoc Comments Be Placed Before or After Annotations in Java?

Explore coding standards for positioning Javadoc comments with annotations in Java. Understand best practices and see examples for clarity.

⦿How to Rename Fields in JSON Output Using Jackson

Learn how to customize JSON field names using Jackson annotations to display id as value and name as label.

⦿How to Access Environment Variables in POM.xml with Maven

Learn how to reference environment variables in your POM.xml while using Maven as a build tool.

⦿Choosing Between Google Guava and Apache Commons Collections for Bidirectional Maps in Java

Explore the differences between Google Guava and Apache Commons Collections for Bidirectional Map implementations in Java. Learn which library to choose and why.

⦿When Should You Implement the Serializable Interface in Java?

Discover when and why to implement the Serializable interface in Java its benefits and best practices for usage.

⦿Resolving the JUnit Version Error in IntelliJ IDEA for Android Projects

Learn how to fix the JUnit version 3.8 or later expected error in IntelliJ IDEA when running tests in Android projects.

⦿How to Efficiently Remove All Null Elements from an ArrayList or String Array?

Learn effective methods to remove null elements from an ArrayList or String array in Java optimizing performance and code clarity.

© Copyright 2025 - CodingTechRoom.com