Thread:
 RE : Suggestion 
 cameronbraid   20 Apr 2005, 22:47 
 Re: RE : Suggestion 
 shangqiao   25 Apr 2005, 20:14 

Comment
Prev. thread 
 Next thread
 
Prev. posting 
 Next posting
From: shangqiao (25 Apr 2005, 20:14) Replies: 0, Views: 37104
Subject: Re: RE : Suggestion
if you want to store the Enum type with int,you can:
    public Object nullSafeGet(ResultSet resultSet, String[] names, 
Object owner)
            throws HibernateException, SQLException {
        Object result = null;
        try {
            int index = resultSet.getInt(names[0]);
            if (!resultSet.wasNull()) {
                Object[] enumValue = enumValues.get(this.getClass());
                if (enumValue == null) {
                    Method method = null;
                    method = clazz.getDeclaredMethod("values", new 
Class[0]);
                    enumValue = (Object[]) method.invoke(null, new 
Object[0]);
                    enumValues.put(this.getClass(), enumValue);
                }
                result = enumValue[index];
            }
        } catch (Exception e) {
            e.printStackTrace();
            result = null;
        }
        return result;
    }

    public void nullSafeSet(PreparedStatement preparedStatement, 
Object value,
            int index) throws HibernateException, SQLException {
        if (null == value) {
            preparedStatement.setNull(index, Types.VARCHAR);
        } else {
            preparedStatement.setInt(index, ((Enum) value).ordinal());
        }
    }
Prev. thread 
 Next thread
 
Prev. posting 
 Next posting
© Copyright 2006, Red Hat Middleware, LLC. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc. [Privacy Policy]