import java.io.Serializable;
import org.hibernate.HibernateException;
public class HairColorEnum extends AbstractEnumToDBValue {
public enum HairColor {
BLOND("Blond"), BRAUN("Braun"), BLACK("Black"), RED("Red"), GRAY("Gray"), WHITE(
"White"), UNKNOWN("");
private String dBvalue;
private HairColor(String dBvalue) {
this.dBvalue = dBvalue;
}
/**
* Returns the value of an Enum field.
*
* @return value of the Enum field
*/
public String getValue() {
return this.dBvalue;
}
/**
* Returns the Enum field to a given value. Returns UNKNOWN if the given
* value does not matches to an Enum field.
*
* @param value
* @return Enum field
*/
public static HairColor getByValue(String value) {
for ( HairColor hairColor : HairColor.values() ) {
if ( hairColor.getValue().equals(value) ) return hairColor;
}
return HairColor.UNKNOWN;
}
}
@Override
public Class returnedClass() {
return HairColor.class;
}
@Override
protected Enum getNullSafeJavaEnumValue(String dbValue) {
HairColor returnValue = null;
// search for the given string db value
for (HairColor value : HairColor.values()) {
if (value.getValue().equals(dbValue)) {
returnValue = value;
}
}
// null safe!!!
if (returnValue == null) {
returnValue = HairColor.UNKNOWN;
}
return returnValue;
}
@Override
protected String getNullSafeDBValue(Enum javaValue) {
if (javaValue == null) {
return HairColor.UNKNOWN.getValue();
} else {
return ((HairColor) javaValue).getValue();
}
}
@Override
public Serializable disassemble(Object value) throws HibernateException {
return (HairColor) value;
}
}
----------------
import java.io.Serializable;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import org.hibernate.HibernateException;
import org.hibernate.usertype.UserType;
public abstract class AbstractEnumToDBValue implements UserType {
public int[] sqlTypes() {
return new int[] { Types.VARCHAR };
}
public abstract Class returnedClass();
public boolean equals(Object x, Object y) throws HibernateException {
return (x == null) ? y == null : x.equals(y);
}
public int hashCode(Object x) throws HibernateException {
return (x == null) ? 0 : x.hashCode();
}
public Object nullSafeGet(ResultSet rs, String[] names, Object owner)
throws HibernateException, SQLException {
String x = rs.getString(names[0]);
return this.getNullSafeJavaEnumValue(x);
}
protected abstract Enum getNullSafeJavaEnumValue(String dbValue);
public void nullSafeSet(PreparedStatement preparedStatement, Object value,
int index) throws HibernateException, SQLException {
preparedStatement.setString(index, this.getNullSafeDBValue((Enum)value));
}
protected abstract String getNullSafeDBValue(Enum javaValue);
public Object deepCopy(Object x) throws HibernateException {
return x;
}
public boolean isMutable() {
return false;
}
public abstract Serializable disassemble(Object value) throws HibernateException;
public Object assemble(Serializable cached, Object owner)
throws HibernateException {
return cached;
}
public Object replace(Object original, Object target, Object owner)
throws HibernateException {
return original;
}
}