Hibernate : How to Use Enum

 

By default Hibernate persists the enumeration value as integer in the database when type param is not specified, In order to store the enum display name, we use a param type, which should be pointed to value in java.sql. Instead of leaving the input open it is a better practice to confine the possible input values in the form of enum. For example, A product can have limited number of state like available, out of stock etc. If we will leave the input out open there are chances of confusion causing data errors eventually. However, if we create the input enum then, changes of data error becomes less.
The details is mentined in the diagram attached.

Java class showing usage of enum

Java class showing usage of enum

 

You might be curious to know how would it persist in the Database table. Below snap shot explains it. Please care of the comments mentioned in the diagrams.

Table view of Enum persisted from Hibernate entity

Table view of Enum persisted from Hibernate entity

 

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s