Return to Snippet

Revision: 5495
at March 12, 2008 12:46 by narkisr


Updated Code
import org.hibernate.HibernateException;
import org.hibernate.usertype.ParameterizedType;
import org.hibernate.usertype.UserType;
import java.io.Serializable;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.Properties;

/** 
* This class implements the Type that will be used in the hbm file, notice that 
* the JavaEnumToString & StringToJavaEnum implementation isn't provided 
* (all they do is to convert Enum<->String in a consistent way, same goes for 
* StringUtils (replaceable with apache commons StringUtils).
*/
public class JavaEnumUserType implements UserType, ParameterizedType {

	private static final int[] SQL_TYPE = new int[]{Types.VARCHAR};

	private Class<? extends Enum> enumClass;


	private static JavaEnumToString javaEnumToString = new JavaEnumToString();

	private static StringToJavaEnum stringToJavaEnum = new StringToJavaEnum();

	public void setParameterValues(Properties parameters) {
		String enumClassName = parameters.getProperty("enumClass");
		try {
			enumClass = Class.forName(enumClassName).asSubclass(Enum.class);
		} catch (ClassNotFoundException e) {
			throw new HibernateException("Class " + enumClassName + " not found ", e);
		}
	}

	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 {
		String value = rs.getString(names[0]);
		if (rs.wasNull() || StringUtils.isNullOrEmpty(value)) {
			return null;
		}
		return stringToJavaEnum.doConvert(value.trim(), enumClass);
	}

	public void nullSafeSet(PreparedStatement st, Object value, int index) throws HibernateException, SQLException {
		if (value == null) {
			st.setNull(index, Types.VARCHAR);
		} else {
			String stringValue = javaEnumToString.doConvert((Enum) value);
			st.setString(index, stringValue);
		}
	}

	public Object replace(Object original, Object target, Object owner)
			throws HibernateException {
		return original;
	}

	public Class returnedClass() {
		return enumClass;
	}

	public int[] sqlTypes() {
		return SQL_TYPE;
	}

}

// For example the following bean:

public class DummyBean{
 
  private DummyEnum dummyProp; 

  public DummyEnum getDummyProp() {
    return dummyProp;
  }

  public void setPageDescriptor(DummyEnum _dummyProp) {
    this.dummyProp = _dummyProp;
  }

}

// will be mapped in this fashion:

<hibernate-mapping>
  <class name="com.dummy.DummyBean" table="DUMMY_TABLE">

   <property name="dummyProp" column="DUMMY_PROP">
    <type name="com.dummy.JavaEnumUserType">
      <param name="enumClass">com.dummy.DummyEnum</param>
    </type>
   </property>
</hibernate-mapping>

Revision: 5494
at March 12, 2008 12:44 by narkisr


Updated Code
import org.hibernate.HibernateException;
import org.hibernate.usertype.ParameterizedType;
import org.hibernate.usertype.UserType;
import java.io.Serializable;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.Properties;

/** 
* This class implements the Type that will be used in the hbm file, notice that 
* the JavaEnumToString & StringToJavaEnum implementation isn't provided 
* (all they do is to convert Enum<->String in a consistent way, same goes for 
* StringUtils (replaceable with apache commons StringUtils).
*/
public class JavaEnumUserType implements UserType, ParameterizedType {

	private static final int[] SQL_TYPE = new int[]{Types.VARCHAR};

	private Class<? extends Enum> enumClass;


	private static JavaEnumToString javaEnumToString = new JavaEnumToString();

	private static StringToJavaEnum stringToJavaEnum = new StringToJavaEnum();

	public void setParameterValues(Properties parameters) {
		String enumClassName = parameters.getProperty("enumClass");
		try {
			enumClass = Class.forName(enumClassName).asSubclass(Enum.class);
		} catch (ClassNotFoundException e) {
			throw new HibernateException("Class " + enumClassName + " not found ", e);
		}
	}

	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 {
		String value = rs.getString(names[0]);
		if (rs.wasNull() || StringUtils.isNullOrEmpty(value)) {
			return null;
		}
		return stringToJavaEnum.doConvert(value.trim(), enumClass);
	}

	public void nullSafeSet(PreparedStatement st, Object value, int index) throws HibernateException, SQLException {
		if (value == null) {
			st.setNull(index, Types.VARCHAR);
		} else {
			String stringValue = javaEnumToString.doConvert((Enum) value);
			st.setString(index, stringValue);
		}
	}

	public Object replace(Object original, Object target, Object owner)
			throws HibernateException {
		return original;
	}

	public Class returnedClass() {
		return enumClass;
	}

	public int[] sqlTypes() {
		return SQL_TYPE;
	}

}

// For example the following bean

public class DummyBean{
 
  private DummyEnum dummyProp; 

  public DummyEnum getDummyProp() {
    return dummyProp;
  }

  public void setPageDescriptor(DummyEnum _dummyProp) {
    this.dummyProp = _dummyProp;
  }

}

// This is how it will look up in our hbm

<hibernate-mapping>
  <class name="com.dummy.DummyBean" table="DUMMY_TABLE">

   <property name="dummyProp" column="DUMMY_PROP">
    <type name="com.dummy.JavaEnumUserType">
      <param name="enumClass">com.dummy.DummyEnum</param>
    </type>
   </property>
</hibernate-mapping>

Revision: 5493
at March 12, 2008 12:43 by narkisr


Updated Code
import org.hibernate.HibernateException;
import org.hibernate.usertype.ParameterizedType;
import org.hibernate.usertype.UserType;
import java.io.Serializable;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.Properties;

/** 
* This class implements the Type that will be used in the hbm file, notice that * the JavaEnumToString & StringToJavaEnum implementation isn't provided 
* (all they do is to convert Enum<->String in a consistent way, same goes for 
* StringUtils (replaceable with apache commons StringUtils).
*/
public class JavaEnumUserType implements UserType, ParameterizedType {

	private static final int[] SQL_TYPE = new int[]{Types.VARCHAR};

	private Class<? extends Enum> enumClass;


	private static JavaEnumToString javaEnumToString = new JavaEnumToString();

	private static StringToJavaEnum stringToJavaEnum = new StringToJavaEnum();

	public void setParameterValues(Properties parameters) {
		String enumClassName = parameters.getProperty("enumClass");
		try {
			enumClass = Class.forName(enumClassName).asSubclass(Enum.class);
		} catch (ClassNotFoundException e) {
			throw new HibernateException("Class " + enumClassName + " not found ", e);
		}
	}

	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 {
		String value = rs.getString(names[0]);
		if (rs.wasNull() || StringUtils.isNullOrEmpty(value)) {
			return null;
		}
		return stringToJavaEnum.doConvert(value.trim(), enumClass);
	}

	public void nullSafeSet(PreparedStatement st, Object value, int index) throws HibernateException, SQLException {
		if (value == null) {
			st.setNull(index, Types.VARCHAR);
		} else {
			String stringValue = javaEnumToString.doConvert((Enum) value);
			st.setString(index, stringValue);
		}
	}

	public Object replace(Object original, Object target, Object owner)
			throws HibernateException {
		return original;
	}

	public Class returnedClass() {
		return enumClass;
	}

	public int[] sqlTypes() {
		return SQL_TYPE;
	}

}

// For example the following bean

public class DummyBean{
 
  private DummyEnum dummyProp; 

  public DummyEnum getDummyProp() {
    return dummyProp;
  }

  public void setPageDescriptor(DummyEnum _dummyProp) {
    this.dummyProp = _dummyProp;
  }

}

// This is how it will look up in our hbm

<hibernate-mapping>
  <class name="com.dummy.DummyBean" table="DUMMY_TABLE">

   <property name="dummyProp" column="DUMMY_PROP">
    <type name="com.dummy.JavaEnumUserType">
      <param name="enumClass">com.dummy.DummyEnum</param>
    </type>
   </property>
</hibernate-mapping>

Revision: 5492
at March 12, 2008 12:42 by narkisr


Updated Code
/** 
This class implements the Type that will be used in the hbm file, notice that the JavaEnumToString & StringToJavaEnum implementation isn't provided (all they do is to convert Enum<->String in a consistent way, same goes for StringUtils (replaceable with apache commons StringUtils).
**/

import org.hibernate.HibernateException;
import org.hibernate.usertype.ParameterizedType;
import org.hibernate.usertype.UserType;

import java.io.Serializable;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.Properties;

public class JavaEnumUserType implements UserType, ParameterizedType {

	private static final int[] SQL_TYPE = new int[]{Types.VARCHAR};

	private Class<? extends Enum> enumClass;


	private static JavaEnumToString javaEnumToString = new JavaEnumToString();

	private static StringToJavaEnum stringToJavaEnum = new StringToJavaEnum();

	public void setParameterValues(Properties parameters) {
		String enumClassName = parameters.getProperty("enumClass");
		try {
			enumClass = Class.forName(enumClassName).asSubclass(Enum.class);
		} catch (ClassNotFoundException e) {
			throw new HibernateException("Class " + enumClassName + " not found ", e);
		}
	}

	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 {
		String value = rs.getString(names[0]);
		if (rs.wasNull() || StringUtils.isNullOrEmpty(value)) {
			return null;
		}
		return stringToJavaEnum.doConvert(value.trim(), enumClass);
	}

	public void nullSafeSet(PreparedStatement st, Object value, int index) throws HibernateException, SQLException {
		if (value == null) {
			st.setNull(index, Types.VARCHAR);
		} else {
			String stringValue = javaEnumToString.doConvert((Enum) value);
			st.setString(index, stringValue);
		}
	}

	public Object replace(Object original, Object target, Object owner)
			throws HibernateException {
		return original;
	}

	public Class returnedClass() {
		return enumClass;
	}

	public int[] sqlTypes() {
		return SQL_TYPE;
	}

}

// For example the following bean

public class DummyBean{
 
  private DummyEnum dummyProp; 

  public DummyEnum getDummyProp() {
    return dummyProp;
  }

  public void setPageDescriptor(DummyEnum _dummyProp) {
    this.dummyProp = _dummyProp;
  }

}

// This is how it will look up in our hbm

<hibernate-mapping>
  <class name="com.dummy.DummyBean" table="DUMMY_TABLE">

   <property name="dummyProp" column="DUMMY_PROP">
    <type name="com.dummy.JavaEnumUserType">
      <param name="enumClass">com.dummy.DummyEnum</param>
    </type>
   </property>
</hibernate-mapping>

Revision: 5491
at March 12, 2008 12:40 by narkisr


Initial Code
/* 
This class implements the Type that will be used in the hbm file, notice that the JavaEnumToString & StringToJavaEnum implementation isn't provided (all they do is to convert Enum<->String in a consistent way, same goes for StringUtils (replaceable with apache commons StringUtils).
*/

import org.hibernate.HibernateException;
import org.hibernate.usertype.ParameterizedType;
import org.hibernate.usertype.UserType;

import java.io.Serializable;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.Properties;

public class JavaEnumUserType implements UserType, ParameterizedType {

	private static final int[] SQL_TYPE = new int[]{Types.VARCHAR};

	private Class<? extends Enum> enumClass;


	private static JavaEnumToString javaEnumToString = new JavaEnumToString();

	private static StringToJavaEnum stringToJavaEnum = new StringToJavaEnum();

	public void setParameterValues(Properties parameters) {
		String enumClassName = parameters.getProperty("enumClass");
		try {
			enumClass = Class.forName(enumClassName).asSubclass(Enum.class);
		} catch (ClassNotFoundException e) {
			throw new HibernateException("Class " + enumClassName + " not found ", e);
		}
	}

	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 {
		String value = rs.getString(names[0]);
		if (rs.wasNull() || StringUtils.isNullOrEmpty(value)) {
			return null;
		}
		return stringToJavaEnum.doConvert(value.trim(), enumClass);
	}

	public void nullSafeSet(PreparedStatement st, Object value, int index) throws HibernateException, SQLException {
		if (value == null) {
			st.setNull(index, Types.VARCHAR);
		} else {
			String stringValue = javaEnumToString.doConvert((Enum) value);
			st.setString(index, stringValue);
		}
	}

	public Object replace(Object original, Object target, Object owner)
			throws HibernateException {
		return original;
	}

	public Class returnedClass() {
		return enumClass;
	}

	public int[] sqlTypes() {
		return SQL_TYPE;
	}

}

// For example the following bean

public class DummyBean{
 
  private DummyEnum dummyProp; 

  public DummyEnum getDummyProp() {
    return dummyProp;
  }

  public void setPageDescriptor(DummyEnum _dummyProp) {
    this.dummyProp = _dummyProp;
  }

}

// This is how it will look up in our hbm

<hibernate-mapping>
  <class name="com.dummy.DummyBean" table="DUMMY_TABLE">

   <property name="dummyProp" column="DUMMY_PROP">
    <type name="com.dummy.JavaEnumUserType">
      <param name="enumClass">com.dummy.DummyEnum</param>
    </type>
   </property>
</hibernate-mapping>

Initial URL

                                

Initial Description
This is an example of how to map a Java 5 enum when using Hibernate ORM.

Initial Title
Java 5 Enum HIbernate mapping

Initial Tags
java

Initial Language
Java