-->
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: Space efficient, future proof Enum mapping?
PostPosted: Thu Jun 29, 2006 5:12 am 
Newbie

Joined: Tue May 23, 2006 12:46 pm
Posts: 17
By default Enums are mapped using their ordinal values. This means that changes to the order in which enums are declared (or inserting new enums in between existing ones) changes their ordinal value and invalidates what's already stored in the database.

For example:

enum Color {
RED, GREEN, BLUE;
}

changed to:

enum Color {
RED, BLUE, ORANGE, GREEN;
}

changes the ordinal of BLUE from 2 to 1 and GREEN from 1 to 3.

So, to protect against changes like this we've started using annotations like this:

@Enumerated(EnumType.STRING)

However, this leads to one VARCHAR for every object in the table.

Is there a way of getting Hibernate to map Enum string values in a normalised way. This would save storage space and protect against changes in ordinal values from code release to code release.

Any help would be greatly appreciated. Thanks.
Ashley


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 30, 2006 8:22 am 
Expert
Expert

Joined: Sat Oct 25, 2003 8:49 am
Posts: 490
Location: Vrhnika, Slovenia
You can use your own UserType - EnumUserType:

Code:
    @Type(
            type = "com.gl.data.types.EnumUserType",
            parameters = {@Parameter(name = "enumClassName", value = "com.gl.moneta.PurchaseState")}
    )
    public PurchaseState getPurchaseState() {
        return purchaseState;
    }

public class EnumUserType implements EnhancedUserType, ParameterizedType {

    private Class<Enum> enumClass;

    public void setParameterValues(Properties parameters) {
        String enumClassName = parameters.getProperty("enumClassName");
        try {
            enumClass = (Class<Enum>)Class.forName(enumClassName);
        }
        catch (ClassNotFoundException cnfe) {
            throw new HibernateException("Enum class not found", cnfe);
        }
    }

    public Object assemble(Serializable cached, Object owner)
            throws HibernateException {
        return cached;
    }

    public Object deepCopy(Object value) throws HibernateException {
        return value;
    }

    public Serializable disassemble(Object value) throws HibernateException {
        return (Enum)value;
    }

    public boolean equals(Object x, Object y) throws HibernateException {
        return x == y;
    }

    public int hashCode(Object x) throws HibernateException {
        return x.hashCode();
    }

    public boolean isMutable() {
        return false;
    }

    public Object nullSafeGet(ResultSet rs, String[] names, Object owner)
            throws HibernateException, SQLException {
        String name = rs.getString(names[0]);
        return rs.wasNull() ? null : Enum.valueOf(enumClass, name);
    }

    public void nullSafeSet(PreparedStatement st, Object value, int index)
            throws HibernateException, SQLException {
        if (value == null) {
            st.setNull(index, Types.VARCHAR);
        } else {
            st.setString(index, ((Enum)value).name());
        }
    }

    public Object replace(Object original, Object target, Object owner)
            throws HibernateException {
        return original;
    }

    public Class returnedClass() {
        return enumClass;
    }

    public int[] sqlTypes() {
        return new int[]{Types.VARCHAR};
    }

    public Object fromXMLString(String xmlValue) {
        return Enum.valueOf(enumClass, xmlValue);
    }

    public String objectToSQLString(Object value) {
        return '\'' + ((Enum)value).name() + '\'';
    }

    public String toXMLString(Object value) {
        return ((Enum)value).name();
    }

}


Change the way it si written and read from db - to optimize it.

Rgds, Ales


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.