Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Hibernate Enum persistence with GenericEnumUserType

807603Jan 27 2008 — edited Jan 27 2008
I am using the custom user type found at

http://www.hibernate.org/272.html

to persist some Enums. I have some domain objects that have compound ids; the ids are composed of 3 (different) enums. sometimes i intentionally have null for one of those enums.

The problem is when I persist and object with a compound key containing a null enum, when I then load that object null for the entire object is returned (instead of just the key component that that should be null). I have a test where i save the object with null key component; I verify in the db via jdbc that object is saved -- its coming out of the database that is broxed.

here are the important methods from the user type
   public Object nullSafeGet(ResultSet rs, String[] names, Object owner) throws HibernateException, SQLException {  
        Object identifier = type.get(rs, names[0]);
        if (rs.wasNull()) {
            return null;
        }
        
        try {
            return valueOfMethod.invoke(enumClass, new Object[] { identifier });
        } catch (Exception e) {
            throw new HibernateException("Exception while invoking valueOf method '" + valueOfMethod.getName() + "' of " +
                    "enumeration class '" + enumClass + "'", e);
        }
    }

    public void nullSafeSet(PreparedStatement st, Object value, int index) throws HibernateException, SQLException {
        try {
            if (value == null) {
//                st.setNull(index, type.sqlType());
                st.setObject(index, null);
            } else {
                Object identifier = identifierMethod.invoke(value, new Object[0]);
                type.set(st, identifier, index);
            }
        } catch (Exception e) {
            throw new HibernateException("Exception while invoking identifierMethod '" + identifierMethod.getName() + "' of " +
                    "enumeration class '" + enumClass + "'", e);
        }
    }
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Feb 24 2008
Added on Jan 27 2008
1 comment
604 views