animesh kumar

Running water never grows stale. Keep flowing!

Posts Tagged ‘Enum

Postgres Enum with Hibernate

with 8 comments

[tweetmeme source=”anismiles” only_single=false

In a running project, we had to map Postgres Enum onto Java Enum using Hibernate. It turned out to be more tricky than we had assumed initially. So, I thought a little tutorial here might save your time if you are stuck in the similar situation.

I am taking an example of Person-Gender mapping where Gender is of enum type.

  1. First, create proper Enums and Tables in Postgres database. Here is the script.
    create type genderType as enum (
    	'MALE', 'FEMALE'
    create table person (
    	personid serial NOT NULL,
    	gender genderType not null
  2. Then, you need to define you Enum class.
    package com.impetus.ilabs.entity;
    public enum GenderType {
        MALE, FEMALE;
  3. Now, define your Entity class like this,
    package com.impetus.ilabs.entity;
    public class Person implements Serializable {
    	private Integer personId;
    	private GenderType gender;
    	public Integer getPersonId() {
    		return personId;
    	public void setPersonId(Integer personId) {
    		this.personId = personId;
    	public GenderType getGender() {
    		return gender;
    	public void setGender(GenderType gender) {
    		this.gender = gender;
    	public String toString() {
    		StringBuilder builder = new StringBuilder();
    		builder.append("Person [gender=");
    		builder.append(", personId=");
    		return builder.toString();
  4. This is the magical part. You need to implement a custom UserType that can understand Enum objects. Here is an implementation of PGEnumUserType.
    package com.impetus.ilabs;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Types;
    import java.util.Properties;
    import org.hibernate.HibernateException;
    import org.hibernate.usertype.EnhancedUserType;
    import org.hibernate.usertype.ParameterizedType;
    import org.postgresql.util.PGobject;
    // This implementation works only with Postgres
    public class PGEnumUserType implements EnhancedUserType, ParameterizedType {
    	// Enum  class under observation
    	private Class<Enum> enumClass;
    	public void setParameterValues(Properties parameters) {
    		String enumClassName = parameters.getProperty("enumClassName");
    		try {
    			enumClass = (Class<Enum>) Class.forName(enumClassName);
    		} catch (ClassNotFoundException cnfe) {
    			throw new HibernateException("Enum class not found", cnfe);
    	public Object assemble(Serializable cached, Object owner)
    			throws HibernateException {
    		return cached;
    	public Object deepCopy(Object value) throws HibernateException {
    		return value;
    	public Serializable disassemble(Object value) throws HibernateException {
    		return (Enum) value;
    	public boolean equals(Object x, Object y) throws HibernateException {
    		return x == y;
    	public int hashCode(Object x) throws HibernateException {
    		return x.hashCode();
    	public boolean isMutable() {
    		return false;
    	public Object nullSafeGet(ResultSet rs, String[] names, Object owner)
    			throws HibernateException, SQLException {
    		Object object = rs.getObject(names[0]);
    		if (rs.wasNull()) {
    			return null;
    		// Notice how Object is mapped to PGobject. This makes this implementation Postgres specific
    		if (object instanceof PGobject) {
    			PGobject pg = (PGobject) object;
    			return Enum.valueOf(enumClass, pg.getValue());
    		return null;
    	public void nullSafeSet(PreparedStatement st, Object value, int index)
    			throws HibernateException, SQLException {
    		if (value == null) {
    			st.setNull(index, Types.VARCHAR); 
    			// UPDATE: To support NULL insertion, change to: st.setNull(index, 1111);
    		} else {
    			// Notice 1111 which java.sql.Type for Postgres Enum
    			st.setObject(index, ((Enum)value), 1111);
    	public Object replace(Object original, Object target, Object owner)
    			throws HibernateException {
    		return original;
    	public Class returnedClass() {
    		return enumClass;
    	public int[] sqlTypes() {
    		return new int[] { Types.VARCHAR }; 
    		// UPDATE: To support NULL insertion, change to: return new int[] { 1111 };
    	public Object fromXMLString(String xmlValue) {
    		return Enum.valueOf(enumClass, xmlValue);
    	public String objectToSQLString(Object value) {
    		return '\'' + ((Enum) value).name() + '\'';
    	public String toXMLString(Object value) {
    		return ((Enum) value).name();
  5. Okay, so the tricky part is over. Now, you need to write Hibernate Mapping xml for Person class.
    <?xml version="1.0"?>
    <!DOCTYPE hibernate-mapping PUBLIC
            "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    	<class name="com.impetus.ilabs.entity.Person" table="person">
    		<id name="personId" type="java.lang.Integer"
    			<generator class="native"></generator>
    		<property name="gender">
    			<type name="com.impetus.ilabs.PGEnumUserType">
    				<param name="enumClassName">com.impetus.ilabs.entity.GenderType</param>
  6. Done! I am including a sample usage class, but you don’t worry about this part, you are good with whatever DAO implementation you might have.
    package com.impetus.ilabs;
    import org.hibernate.Session;
    import org.hibernate.SessionFactory;
    import org.hibernate.cfg.Configuration;
    import com.impetus.ilabs.entity.GenderType;
    import com.impetus.ilabs.entity.Person;
    public class App {
    	private static final SessionFactory sessionFactory = buildSessionFactory();
    	private static SessionFactory buildSessionFactory() {
    		try {
    			return new Configuration().configure().buildSessionFactory();
    		} catch (Throwable ex) {
    			throw new ExceptionInInitializerError(ex);
    	private static void save (Person person) {
    		Session session = sessionFactory.openSession();
    		// create object;
    	private static void load (int id) {
    		Session session = sessionFactory.openSession();
    		System.out.println (session.load(Person.class, id));
    	public static void main(String[] args) {
    		// create object
    		Person person = new Person();
    		// load object

Enjoy! By the way, the whole thing that is described above is also available as maven project to download.

Written by Animesh

August 4, 2010 at 3:48 pm

Posted in Technology

Tagged with , , , ,