Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I've created a similar class like the one suggested by hibernate only that is configurable and there is no need to create a new type only for this persistence.</p> <p>Can be used like</p> <pre><code>@Type(type = "ro.raisercostin.hibernate.EnumUserType", parameters = @Parameter(name = "type", value = "DealType")) DealType dealType; </code></pre> <p>I added an implementation of ParameterizedType to support the passed parameter.</p> <pre><code>public class EnumUserType implements UserType, ParameterizedType { private static final int[] SQL_TYPES = { Types.VARCHAR }; private Class clazz = null; public EnumUserType() { } @Override public void setParameterValues(Properties parameters) { String className = (String) parameters.get("type"); try { this.clazz = Class.forName(className); } catch (ClassNotFoundException e) { throw new RuntimeException("Couldn't get the class for name [" + className + "].", e); } } 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]); Object 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 { preparedStatement.setString(index, ((Enum) value).name()); } } 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); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload