Maven dependencies used all over JPA tutorials same. Please follow "Setting up Project" section to understand about project configuration.
persistence.xml and other code files reused from How to Persist an Entity in data base using JPA?
Each Sample application from download includes SQL file also.

Java Enum :

  • In Java we will use Enums to represent group of named constants.
  • If you use enums instead of integers (or String or Char) to represent constants, you increase compile-time checking and avoid errors from passing in invalid constants.


Mapping Enum in JPA :

  • JPA supports converting database data to Java MARKDOWN_HASH87b1468f5b22fdb2b21193e2ec7fd9cdMARKDOWN_HASH types and  vice versa via the MARKDOWN_HASH0033b91c5b20cce72016109a05bdf252MARKDOWN_HASH annotation.
  • Serialization and Deserialization involves to convert enum types to database types.
  • JPA allows enums to save either String type or Ordinal type.
  • Within the enum the order of constants also important, we can specify this by ordinal value. Ordinal value starts from zero.
  • From the below image EMPLOYEE ordinal is “0” and STUDENT ordinal is “1”. Ordinal value can be  retrieved by using ordinal().

Mapping Enum Type :

UserType Enum :

User.Java :

  • In, from above code snippet line number 23 describe the mapping of UserType enum.
  • If you want to save the position of Enum constant use EnumType.ORDINAL.
Download Application – (11 KB)