Hi
this can done by using UserType
public enum Color {
RED,
BLUE,
PINK
}
import java.io.Serializable;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import org.hibernate.Hibernate;
import org.hibernate.HibernateException;
import org.hibernate.usertype.UserType;
public class ColorUserType implements UserType {
private static final int[] TYPES = { Types.VARCHAR };
public int[] sqlTypes() {
return TYPES;
}
public Class returnedClass() {
return Color.class;
}
public boolean equals(Object arg0, Object arg1) throws HibernateException {
if (arg0 instanceof Color) {
Color new_name = (Color) arg0;
if (arg1 instanceof Color) {
Color new_name1 = (Color) arg1;
return new_name1.equals(new_name);
}
}
return false;
}
public int hashCode(Object arg0) throws HibernateException {
return arg0.hashCode();
}
public Object nullSafeGet(ResultSet rs, String[] names, Object arg2)throws HibernateException, SQLException {
Color dbValue = Color.valueOf((String) Hibernate.STRING.nullSafeGet(rs, names[0]));
return dbValue;
}
public void nullSafeSet(PreparedStatement st, Object value, int index)
throws HibernateException, SQLException {
if ( value != null ) {
String v =((Color)value).toString();
Hibernate.STRING.nullSafeSet(st, v, index);
}
else {
Hibernate.STRING.nullSafeSet(st, value, index);
}
}
public Object deepCopy(Object arg0) throws HibernateException {
return arg0;
}
public boolean isMutable() {
return false;
}
public Serializable disassemble(Object value) throws HibernateException {
return (Serializable) deepCopy(value);
}
public Object assemble(Serializable arg0, Object arg1)
throws HibernateException {
return deepCopy(arg0);
}
public Object replace(Object arg0, Object arg1, Object arg2)
throws HibernateException {
return deepCopy(arg0);
}
}
|