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