-->
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.  [ 3 posts ] 
Author Message
 Post subject: EnumType question
PostPosted: Wed Jan 30, 2008 10:23 am 
Beginner
Beginner

Joined: Sat Oct 20, 2007 8:28 am
Posts: 28
Hi can i overirde the enumtype.ordnial/string somehow ?
for example can i make .string persist the tostring method
or point the .ordinal to a private Integer member within the enum ?
thanks


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 30, 2008 12:25 pm 
Regular
Regular

Joined: Mon Aug 20, 2007 6:47 am
Posts: 74
Location: UK
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;
   }
   
}



Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 01, 2008 6:15 am 
Beginner
Beginner

Joined: Sat Oct 20, 2007 8:28 am
Posts: 28
Did the trick , thanks .


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.