Controlling domain data with Hibernate and Enum
It is common to have a field into the table that his domain is no bigger to created a domain table just for that field and let it “free” as a String is a bit insecure.
A possible solution - if possible in your company - could be the creation of a big table for all simple domain data such as: gender(M/F) conditional(Yes/No) and so on.
But talking about Hibernate and Java (1.5+) we could use a more sophisticated solution such as Enuns.
Let’s start our sample defining the Enum and the Client class:
The annotation @Enumerated has just one param and it can had two values:
- EnumType.ORDINAL
- EnumType.STRING
The difference between them are not visible from the Object’s point of view. For this field will be associated an Enum with all it’s structure.
However, the difference between ORDINAL and STRING can be seen when you take a look at the database. When you define ORDINAL the position defined in Enum for that value will be stored. Starting from zero (zero based). For instance if you define the EnumType as ORDINAL and create an object like this:
And store it in database the record stored will looks like this:

On the other hand, if the definition of the EnumType is defined as STRING, for the same creation code will produce this record in the database:



