JavaChap Blog Java and Technology musings for the masses

11Jul/110

Enumeration Mapping in Hibernate

In this simple tutorial i will show you, how to map a Java 5 Enumeration (enum) to a varchar column in Hibernate. The use-case that i'm going to demonstrate is very simple, I have a User class which has a reference to an Enumeration Gender.

Gender Enumeration
package com.javachap.domain.user;
/**
 * Male or Female. Used to customize Account messages (e.g. he or she usage) and
 * to choose the default profile picture.
 */
public enum Gender {
    Male, Female;
}
User Domain:
package com.javachap.domain.user;

public class User {

    private String firstName;
    private String lastName;
    private Gender gender;
    private String email

    /**
     * Gets the <code>User</code>'s firstName
     *
     * @return the firstName
     */
    public String getFirstName() {
        return firstName;
    }

    /**
     * Sets the <code>User</code>'s firstName
     *
     * @param firstName the firstName to set
     */
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    /**
     * Gets the <code>User</code>'s lastName
     *
     * @return the lastName
     */
    public String getLastName() {
        return lastName;
    }

    /**
     * Sets the <code>User</code>'s lastName
     *
     * @param lastName the lastName to set
     */
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    /**
     * Gets the <code>User</code>'s email
     *
     * @return the email
     */
    public String getEmail() {
        return email;
    }

    /**
     * Sets the <code>User</code>'s email
     *
     * @param email the email to set
     */
    public void setEmail(String email) {
        this.email = email;
    }
}
Hibernate Mapping (HBM):
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
  <class table="user" name="com.javachap.domain.user.User">

    <id name="uid" column="usr_uid">
      <generator class="increment"/>
    </id>

    <property name="firstName" type="java.lang.String" column="usr_first_name"/>

    <property name="lastName" type="java.lang.String" column="usr_last_name" />

    <property name="gender" column="usr_gender" not-null="true">
	  <type name="org.hibernate.type.EnumType">
         <param name="enumClass">com.javaachap.domain.user.Gender</param>
         <param name="type">12</param>
      </type>
    </property>

    <property name="email" type="java.lang.String" column="usr_email"/>

  </class>
</hibernate-mapping>

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.Types. Since we want to store the enum display name in database we have mapped it to 12 which is equivalent to java.sql.Types.VARCHAR.

Hibernate Annotation Based Mapping:

If you prefer convention over configuration, below is the way to map an enum using annotation.

public class User {

   @Enumerated(EnumType.STRING)
   Gender gender

}
27Dec/0831

CRUD Application : Struts 1, Hibernate and MySQL

Here is my Sample Struts, Hibernate CRUD application. This can act as starting point for those who are starting to build applications on hibernate and struts. I know struts 1 is sort of out dated, but i still think there are lot of legacy applications running on Struts 1.

This application has the following features.

  • Basic Create, Update and Delete Operations
  • Security (Login Support)
  • Validation (Struts Validations)
  • Internationalization and Localization (Look at the Links on the top of page, you can change the language of the application).
  • Ant build script to build and deploy the application.

Download the source code leadapp.zip (5.66 Mb)

Building and deploying the application

  • Create a schema/user using the schema.sql  file in src/database
  • Open build.properties and modify "tomcat.home" property to point your tomcat home directory
  • Database username/password can be configured in the hibernate config file \WebRoot\WEB-INF\hibernate.cfg.xml
  • Issue "ant clean deploy" command to build and deploy the application to tomcat.
  • Access the application http://localhost:8080/leadapp  (assuming tomcat is running on 8080 port)
  • username/password is user@javachap.com/javachap

I hosted this application on stax.net, access it from here http://hw4999.dvkvarma.staxapps.net, please write in the comments section if you have any problems running the application.

Application Screenshots

Login Page

Lead Listing Page

Lead Create Page