-->
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.  [ 1 post ] 
Author Message
 Post subject: java 5 enum (yet another)
PostPosted: Mon Mar 21, 2005 9:07 pm 
Expert
Expert

Joined: Sat Jun 12, 2004 4:49 pm
Posts: 915
I try java 5 enum like http://www.hibernate.org/272.html
My problem is that is unusable for constants :
0,1,2 ... or 'Fat cat' etc

This is my solution :

mapping is same in wiki 272

I declare one interface (enum must implement this ) :
Code:
package yu.co.snpe.dbtable.model.hibernate.type;

import java.util.List;

public interface StringEnum {
   
    public abstract String getValue();

}


enum example (I have database column with values
0 - Tekuci racun
1 - Stedna knjizica
2 - Blagajna

0,1 or 2 are values in database column

Code:
package yu.co.snpe.dbtable.model.hibernate.type;

import java.util.Arrays;
import java.util.List;

public enum NacinIsplate implements StringEnum {
   
    TekuciRacun("0"),
    StednaKnjizica("1"),
    Blagajna("2");
   
    private String value;
   
    NacinIsplate(String value) {
        this.value = value;
    }
   
    public String getValue() {
        return value;
    }
}


and type :
Code:
package yu.co.snpe.dbtable.model.hibernate.type;

import java.io.Serializable;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.Properties;

import org.hibernate.HibernateException;
import org.hibernate.usertype.ParameterizedType;
import org.hibernate.usertype.UserType;

/**
* @author snpe
*/
public class StringEnumUserType implements UserType, ParameterizedType {

    private Class enumClass;

    public void setParameterValues(Properties parameters) {
        String enumClassName = parameters.getProperty("enumClass");
        try {
            enumClass = Class.forName(enumClassName);
        } catch (ClassNotFoundException cnfe) {
            throw new HibernateException("Enum class " + enumClassName
                    + " not found", cnfe);
        }
        if (!StringEnum.class.isAssignableFrom(enumClass) || !Enum.class.isAssignableFrom(enumClass))
            throw new HibernateException("Enum class " +  enumClassName + " must be enum and implements StringEnum");
                   
    }

    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]);
        if (rs.wasNull())
            return null;
        return getEnum(name);
    }

    private Object getEnum(String name) {
        Object[] enums = enumClass.getEnumConstants();
        for (Object object : enums) {
            if (object instanceof StringEnum) {
                StringEnum stringEnum = (StringEnum) object;
                if (stringEnum.getValue().equals(name))
                    return object;
            }
        }
        throw new HibernateException("bad enum value in database");
    }

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

    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 };
    }

}


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

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.