AbstractEnumUserType.java

  1. package org.itracker.persistence.dao;

  2. import org.hibernate.HibernateException;
  3. import org.hibernate.usertype.EnhancedUserType;
  4. import org.hibernate.usertype.ParameterizedType;

  5. import java.io.Serializable;
  6. import java.util.Properties;

  7. /**
  8.  * Base class for custom Hibernate UserTypes to persist a Java 5 enum
  9.  * constant.
  10.  * <p/>
  11.  * <p>This is a parameterized type, which requires the following parameter :
  12.  * <code>enumClassName</code> = fully qualified name of the enum class
  13.  * to persist. </p>
  14.  * <p/>
  15.  * <p>Example of inline mapping : </p>
  16.  * <pre>
  17.  *  <property name='suit'>
  18.  *   <column name="suit"/>
  19.  *   <type name="EnumUserType">
  20.  *     <param name="enumClassName">com.company.project.Suit</param>
  21.  *   </type>
  22.  *  </property>
  23.  * </pre>
  24.  * <p/>
  25.  * <p>Example of typedef : </p>
  26.  * <pre>
  27.  *  <typedef name="suit" class='EnumUserType'>
  28.  *    <param name="enumClassName">com.company.project.Suit</param>
  29.  *  </typedef>
  30.  *
  31.  *  <class ...>
  32.  *   <column name="suit"/>
  33.  *   <property name='suit' type='suit'/>
  34.  *  </class>
  35.  * </pre>
  36.  *
  37.  * @author johnny
  38.  */
  39. public abstract class AbstractEnumUserType
  40.         implements EnhancedUserType, ParameterizedType {

  41.     @SuppressWarnings("unchecked")
  42.     protected Class<? extends Enum> enumClass;

  43.     public AbstractEnumUserType() {
  44.     }

  45.     @SuppressWarnings("unchecked")
  46.     public void setParameterValues(Properties parameters) {
  47.         String enumClassName = parameters.getProperty("enumClassName");
  48.         try {
  49.             this.enumClass = (Class<? extends Enum>) Class.forName(enumClassName);
  50.         } catch (ClassNotFoundException ex) {
  51.             throw new HibernateException("Enum class not found", ex);
  52.         }
  53.     }

  54.     public Object assemble(Serializable cached, Object owner)
  55.             throws HibernateException {
  56.         return cached;
  57.     }

  58.     public Object deepCopy(Object value) throws HibernateException {
  59.         return value;
  60.     }

  61.     public Serializable disassemble(Object value) throws HibernateException {
  62.         return (Enum<?>) value;
  63.     }

  64.     public boolean equals(Object x, Object y) throws HibernateException {
  65.         return (x == y);
  66.     }

  67.     public int hashCode(Object x) throws HibernateException {
  68.         return x.hashCode();
  69.     }

  70.     public boolean isMutable() {
  71.         return false;
  72.     }

  73.     public Object replace(Object original, Object target, Object owner)
  74.             throws HibernateException {
  75.         return original;
  76.     }

  77.     public Class<?> returnedClass() {
  78.         return this.enumClass;
  79.     }

  80. }