Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enumerations in Hibernate

It is often useful to have a field in a DAO whose value comes from a Java enumeration. A typical example is a login DAO where you usually have a field that characterises the user as "NORMAL" or "ADMIN". In Hibernate, I would use the following 2 objects to represent this relationship in a (semi-)typesafe way:

class User {     String username;     String passwd;     UserType type; }  class UserType {     private enum Type {ADMIN, NORMAL};     private String type;      //Setters/Getters for Hibernate     public void setType(String type);     public String getType();      //Setters/Getters for user     public void setUserType(UserType.Type t);     public UserType.Type getUserType();      public static UserType fromType(UserType.Type t); } 

This works, but I find the UserType class ungly and requiring too much bureaucracy just to store a couple of values. Ideally, Hibernate should support enum fields directly and would create an extra table to store the enumeration values.

My question is: Is there any way to directly map an enumeration class in Hibernate? If not, is my pattern for representing enumerations good enough or am I missing something? What other patterns do people use?

like image 816
Georgios Gousios Avatar asked Jan 06 '09 15:01

Georgios Gousios


People also ask

What is the point of enumerations in Java?

Enums are used to create our own data type like classes. The enum data type (also known as Enumerated Data Type) is used to define an enum in Java. Unlike C/C++, enum in Java is more powerful. Here, we can define an enum either inside the class or outside the class.

What is the keyword of enumerations?

The keyword 'enum' is used to declare new enumeration types in C and C++. Following is an example of enum declaration. // The name of enumeration is "flag" and the constant // are the values of the flag.

Can enumerations have methods?

The enum class body can include methods and other fields. The compiler automatically adds some special methods when it creates an enum. For example, they have a static values method that returns an array containing all of the values of the enum in the order they are declared.

Are enumerations classes?

Yes enum are type of Java Class. The values of an enum are the only possible instances of this class.


2 Answers

using hibernate or JPA annotations:

class User {    @Enumerated(EnumType.STRING)    UserType type } 

UserType is just a standard java 5 enum.

I can't imagine this is just limited to just annotations but I don't actually know how to do this with hbm files. It may be very version dependant, I'm guessing but I'm pretty sure that hibernate 3.2+ is required.

edit: it is possible in a hbm, but is a little messy, have a look at this forum thread

like image 150
Gareth Davis Avatar answered Nov 09 '22 00:11

Gareth Davis


From the Hibernate documentation: http://www.hibernate.org/272.html

You can create a new typedef for each of your enums and reference the typedefs in the property tag.

Example Mapping - inline <type> tag

  <property name='suit'>     <type name="EnumUserType">       <param name="enumClassName">com.company.project.Suit</param>     </type>   </property> 

Example Mapping - using <typedef>

  <typedef name="suit" class='EnumUserType'>       <param name="enumClassName">com.company.project.Suit</param>   </typedef>    <class ...>     <property name='suit' type='suit'/>   </class> 
like image 35
Craig Avatar answered Nov 09 '22 01:11

Craig