Hibernate如何映射枚举类型 (三)

2014-11-24 10:38:30 · 作者: · 浏览: 3
return x.equals(y);
}
}

public abstract class OrdinalEnumUserType> implements UserType {

protected Class clazz;

protected OrdinalBasedEnumUserType(Class clazz) {
this.clazz = clazz;
}

private static final int[] SQL_TYPES = {Types.NUMERIC};
public int[] sqlTypes() {
return SQL_TYPES;
}

public Class< > returnedClass() {
return clazz;
}

public E nullSafeGet(ResultSet resultSet, String[] names, Object owner)
throws HibernateException, SQLException {

//Hibernate.STRING.nullSafeGet(rs, names[0])
int index = resultSet.getInt(names[0]);
E result = null;
if (!resultSet.wasNull()) {
result = clazz.getEnumConstants()[index];
}
return result;
}

public void nullSafeSet(PreparedStatement preparedStatement,
Object value,int index) throws HibernateException, SQLException {
if (null == value) {
preparedStatement.setNull(index, Types.NUMERIC);
} else {
//Hibernate.String.nullSafeSet(st, sex, index);
preparedStatement.setInt(index, ((E)value).ordinal());
}
}

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

public boolean isMutable() {
return false;
}

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

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

public Object replace(Object original, Object target, Object owner)
throws HibernateException {
return original;
}
public int hashCode(Object x) throws HibernateException {
return x.hashCode();
}
public boolean equals(Object x, Object y) throws HibernateException {
if (x == y)
return true;
if (null == x || null == y)
return false;
return x.equals(y);
}
}