Yes, use a UserType with methods like these:
Code:
public Object nullSafeGet(ResultSet resultSet, String[] names, Object owner) throws HibernateException, SQLException {
Long value = new Long(resultSet.getInt(names[0]));
MyEnum result = null;
if (!resultSet.wasNull()) {
result = MyEnum.getById(value);
}
return result;
}
public void nullSafeSet(PreparedStatement preparedStatement, Object value, int index) throws HibernateException, SQLException {
if (null == value) {
preparedStatement.setNull(index, Types.INTEGER);
} else {
preparedStatement.setInt(index, ((MyEnum)value).id);
}
}
For an enum like:
Code:
public enum MyEnum implements Serializable {
VALUE1(1),VALUE2(2);
public final Integer id;
private FieldDataType(Integer id) {
this.id = id;
}
public static MyEnum getById(Integer id) {
MyEnum[] values = MyEnum.values();
for(MyEnum next : values) {
if(next .id == id) {
return next ;
}
}
return null;
}
}