hibernate 带下标值的枚举类型的映射

1 对于普通的枚举值映射 可以使用注解@Enumerated即可

2 当要使用带有下标值的枚举类型时,可以通过注解@Type指定;不过在@Type注解中,要指定具体的类型;此类型要继承hibernate提供的UserType接口,相当于自定义一种类型。 实现UserType接口中的方法,此方法可以帮助hibernate在保存对象属性为枚举时,把枚举值转换成对应的值持久化到数据库。并且在查询对象属性为枚举值,如何把数据库中的值展现成枚举值。

3 提供代码例子
枚举类
public enum UserType {

/**
* 管理员
*/
Admin(1),

/**
* 普通用户
*/
CommonUser(2);

UserType(int value) {
this.value = value;
}

private int value;

public int getValue() {
return value;
}

public static UserType valueOf(int value) {
for (UserType userType : UserType.values()) {
if (userType.getValue() == value) {
return userType;
}
}
throw new OperateFailException("枚举类不支持数字:" + value);
}

public static UserType nameOf(String name) {
for (UserType userType : UserType.values()) {
if (userType.toString().equals(name)) {
return userType;
}
}
throw new OperateFailException("枚举类不支持字面值:" + name);
}
}

抽象类继承UserType接口
public abstract class IntegerEumuUserType<T extends Enum<T>> implements
UserType, Serializable {

/**
*
*/
private static final long serialVersionUID = -1413528409533536144L;

private static int[] TYPES = new int[]{Types.INTEGER};

Class<T> clazz = null;

@SuppressWarnings(value="unchecked")
IntegerEumuUserType() {
clazz = (Class<T>)((ParameterizedType)this.getClass().getGenericSuperclass()).getActualTypeArguments()[0];
}

public Object assemble(Serializable arg0, Object arg1)
throws HibernateException {
return arg0;
}

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

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

public boolean equals(Object x, Object y) throws HibernateException {
if (x == y) {
return true;
} else {
x = (Integer)x;
y = (Integer)y;
if (x.toString().equals(y.toString())) {
return true;
}
}
return false;
}

public int hashCode(Object arg0) throws HibernateException {
return arg0.hashCode();
}

public boolean isMutable() {
return false;
}

public Object nullSafeGet(ResultSet rs, String[] names, Object value)
throws HibernateException, SQLException {
T t = null;
int index = rs.getInt(names[0]);
if (rs.wasNull()) {
t = this.indexOf(index);
}
return t;
}

@SuppressWarnings(value="unchecked")
public void nullSafeSet(PreparedStatement preparedStatement, Object value, int index)
throws HibernateException, SQLException {
if (value == null) {
preparedStatement.setNull(index, Types.INTEGER);
} else {
preparedStatement.setInt(index, this.index((T)value));
}
}

public Object replace(Object original, Object target, Object owner)
throws HibernateException {
return original;
}

public Class<Integer> returnedClass() {
return Integer.class;
}

public int[] sqlTypes() {
return TYPES;
}

public abstract int index(T t);

public abstract T indexOf(int index);

}

当使用具体的枚举类型,只需要创建对应的type即可
public class UserTypeUserType extends IntegerEumuUserType<UserType> {

/**
*
*/
private static final long serialVersionUID = 8763306981292415942L;

@Override
public int index(UserType t) {
return t.getValue();
}

@Override
public UserType indexOf(int index) {
return UserType.valueOf(index);
}

}

你可能感兴趣的:(Hibernate)