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

本文介绍如何在Hibernate中使用枚举值映射。包括使用@Enumerated注解进行基本映射,以及通过实现UserType接口来自定义带有下标值的枚举类型的映射方式。提供了具体的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值