public void setParameterValues(Properties parameters) { String enumClassName = parameters.getProperty("enumClass"); try { enumClass = Class.forName(enumClassName).asSubclass(Enum.class); } catch (ClassNotFoundException cfne) { throw new HibernateException("Enum class not found", cfne); } String identifierMethodName = parameters.getProperty("identifierMethod", DEFAULT_IDENTIFIER_METHOD_NAME); try { identifierMethod = enumClass.getMethod(identifierMethodName, new Class[0]); identifierType = identifierMethod.getReturnType(); } catch (Exception e) { throw new HibernateException("Failed to obtain identifier method", e); } type = (NullableType) TypeFactory.basic(identifierType.getName()); if (type == null) throw new HibernateException("Unsupported identifier type " + identifierType.getName()); sqlTypes = new int[] {type.sqlType()}; String valueOfMethodName = parameters.getProperty("valueOfMethod", DEFAULT_VALUE_OF_METHOD_NAME); try { valueOfMethod = enumClass.getMethod(valueOfMethodName, new Class[] {identifierType}); } catch (Exception e) { throw new HibernateException("Failed to obtain valueOf method", e); } }
public void nullSafeSet(PreparedStatement st, Object value, int index) throws HibernateException, SQLException { try { if (value == null) { st.setNull(index, type.sqlType()); } else { Object identifier = identifierMethod.invoke(value, new Object[0]); type.set(st, identifier, index); } } catch (Exception e) { throw new HibernateException( "Exception while invoking identifierMethod '" + identifierMethod.getName() + "' of " + "enumeration class '" + enumClass + "'", e); } }
public Object nullSafeGet(ResultSet rs, String[] names, Object owner) throws HibernateException, SQLException { Object identifier = type.get(rs, names[0]); if (rs.wasNull()) { return null; } try { return valueOfMethod.invoke(enumClass, new Object[] {identifier}); } catch (Exception e) { throw new HibernateException( "Exception while invoking valueOf method '" + valueOfMethod.getName() + "' of " + "enumeration class '" + enumClass + "'", e); } }