Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp
It's easy to customize the persistence of enum by using *EnumUserType. But how to achieve the save effect with annotation?
For example, I have a enum like this:
Code:
public enum OrderType {
    RecOrder,
    ConsOrder;
    
    /**
     * For some reason, we don't want to use the ordinal() value as the   
     * persited value, so we add a new property index to the enum and 
     * will persist the index value into db.
     */
    private int index;
    public OrderType(int index) {
        this.index = index;
    }
    
    public static OrderType indexOf(int index) {
        for (OrderType orderType: OrderType.values()) {
            if (orderType.index == index) {
                return orderType;
            }
        }
        throw new RuntimeException("No such index value: " + index);
    }
    
    public final int index() {
        return this.index;
    }
}
When config with xml, we can define a OrderTypeEnunUserType to get what I need:
Code:
public class OrderTypeUserType implements UserType {
    
    @Override
    public Object assemble(Serializable cached, Object owner) throws HibernateException {
        return cached;
    }
    @Override
    public Object deepCopy(Object value) throws HibernateException {
        return value;
    }
    @Override
    public Serializable disassemble(Object value) throws HibernateException {
        return (Serializable)value;
    }
    @Override
    public boolean equals(Object x, Object y) throws HibernateException {
        return x == y;
    }
    @Override
    public int hashCode(Object x) throws HibernateException {
        return x.hashCode();
    }
    @Override
    public boolean isMutable() {
        return false;
    }
    @Override
    public Object nullSafeGet(ResultSet rs, String[] names, Object owner) throws HibernateException, SQLException {
        int indexValue= (Integer) Hibernate.INTEGER.nullSafeGet(rs, names[0]);
        return OrderType.indexOf(indexValue);
    }
    @Override
    public void nullSafeSet(PreparedStatement st, Object value, int index) throws HibernateException, SQLException {
        int indexValue = 0;
        if (value != null) {
            indexValue = ((OrderType) value).index();
        }
        Hibernate.INTEGER.nullSafeSet(st, indexValue, index);
    }
    @Override
    public Object replace(Object original, Object target, Object owner) throws HibernateException {
        return target;
    }
    @SuppressWarnings("unchecked")
    @Override
    public Class returnedClass() {
        return OrderType.class;
    }
    @Override
    public int[] sqlTypes() {
        return new int[] {Types.INTEGER};
    }
}
Then I can config like this in .hbm.xml:
Code:
<property name="orderType" type="example.order.OrderTypeUserType"/>
I cannot use @Enumerated(EnumType.ORDINAL) or @Enumerated(EnumType.STRING) because I use the index property for persistence, not use ordinate or name property. 
But how should I config it with annotation?