-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: How to config enum with annotation as the *EnumUserType do?
PostPosted: Thu Nov 13, 2008 4:59 am 
Newbie

Joined: Thu Nov 13, 2008 3:57 am
Posts: 3
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?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 13, 2008 5:45 am 
Regular
Regular

Joined: Mon Aug 20, 2007 6:47 am
Posts: 74
Location: UK
It's just as easy as with xml config:

Code:

@Column(name = "ORDER_TYPE")
@Type(type="example.order.OrderTypeUserType")
private OrderType orderType;



Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.