[jboss-user] [EJB 3.0] - Re: @Enumerated with @ID Usage Issue

fkifle do-not-reply at jboss.com
Mon Sep 17 17:14:33 EDT 2007


Ok.  I found a work around.  I would really appreciate it if someone ran into the same issue and if they have solved it. 
I created enum wrapper class that extends hibernate UserType class:


  | public class EnumVarcharUserType<E extends Enum<E>> implements UserType {
  |     private Class<E> clazz = null;
  | 
  |     protected EnumVarcharUserType(Class<E> c) {
  |         this.clazz = c;
  |     }
  | 
  |     private static final int[] SQL_TYPES = {Types.VARCHAR};
  | 
  |     public int[] sqlTypes() {
  |         return SQL_TYPES;
  |     }
  | 
  |     public Class returnedClass() {
  |         return clazz;
  |     }
  | 
  |     public Object nullSafeGet(ResultSet resultSet, String[] names, Object owner) throws
  |                                                                                  HibernateException,
  |                                                                                  SQLException {
  |         String name = resultSet.getString(names[0]);
  |         E result = null;
  |         if (!resultSet.wasNull()) {
  |             result = Enum.valueOf(clazz, name);
  |         }
  |         return result;
  |     }
  | 
  |     public void nullSafeSet(PreparedStatement preparedStatement, Object value, int index)
  |             throws HibernateException, SQLException {
  |         if (null == value) {
  |             preparedStatement.setNull(index, Types.VARCHAR);
  |         }
  |         else if (value instanceof Enum) {
  |             preparedStatement.setString(index, ((Enum) value).name());
  |         }
  |         else if (value instanceof String) {
  |             preparedStatement.setString(index, (String) value);
  |         }
  |     }
  | 
  |     public Object deepCopy(Object value) throws HibernateException {
  |         return value;
  |     }
  | 
  |     public boolean isMutable() {
  |         return false;
  |     }
  | 
  |     public Object assemble(Serializable cached, Object owner) throws HibernateException {
  |         return cached;
  |     }
  | 
  |     public Serializable disassemble(Object value) throws HibernateException {
  |         return (Serializable) value;
  |     }
  | 
  |     public Object replace(Object original, Object target, Object owner) throws HibernateException {
  |         return original;
  |     }
  | 
  |     public int hashCode(Object x) throws HibernateException {
  |         return x.hashCode();
  |     }
  | 
  |     public boolean equals(Object x, Object y) throws HibernateException {
  |         if (x == y) {
  |             return true;
  |         }
  |         if (null == x || null == y) {
  |             return false;
  |         }
  |         return x.equals(y);
  |     }
  | }
  | 
  | --------------------------------
  | public class InspectionPersonnelTypeUserType
  |         extends EnumVarcharUserType<InspectionPersonnelTypeEnum> {
  |     public InspectionPersonnelTypeUserType() {
  |         super(InspectionPersonnelTypeEnum.class);
  |     }
  | 
So I replaced all instance of InspectionPersonnelTypeEnum with this new wrapped class InspectionPersonnelTypeUserType.

View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4085260#4085260

Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4085260



More information about the jboss-user mailing list