Enumeration and custom data type
In this section, we see how to map "constants" to enumerated types. Constants are considered a small finite set of data elements, such as gender (Male, Female) or days of the week, such as Monday, Tuesday, and so on. Hibernate supports using enumerated types for mapping database columns containing data in a finite closed set. (The definition of a closed set comes straight from set theory.)
If you don't wish to use enumerated types, you can create a custom data type to map your data to a primitive type in Java, and you'll see that after we discuss the enumerated type.
Enumerated type
You can define an enumeration in Java and let Hibernate handle the mapping of "constants". JPA supports two kinds of enum
: ORDINAL
and STRING
. If you use the ORDINAL
enumeration type, the data in the mapped column is assumed to be an integer. You can guess how the STRING
enumeration type behaves:
public enum WeekDay { Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday...