我在这里发现了一些类似的问题,但不完全相同。
如果我的枚举值不是序数,我如何将类字段映射到该枚举类型?
---数据库级别 - 查找表值
insert into ShowInfoLookup(ID, Name) values (0 | 64, 'Hide (auto set by the system)');
insert into ShowInfoLookup(ID, Name) values (1 | 64, 'Show (auto set by the system)');
insert into ShowInfoLookup(ID, Name) values (0 | 128, 'Hide (manually set)');
insert into ShowInfoLookup(ID, Name) values (1 | 128, 'Show (manually set)');
---枚举的Java代码
package com.test.constants;
public enum ShowInfoLookup {
Invalid(0),
HideAutoSet(Value.HIDE | Value.AUTO_SET), // 64
ShowAutoSet(Value.SHOW | Value.AUTO_SET), // 65
HideManuallySet(Value.HIDE | Value.MANUALLY_SET), // 128
ShowManuallySet(Value.SHOW | Value.MANUALLY_SET); // 129
private static class Value {
private static final int HIDE = 0; // false - hide
private static final int SHOW = 1; // true - show
private static final int AUTO_SET = 64;
private static final int MANUALLY_SET = 128;
}
private int value = 0;
private ShowInfoLookup(int value) {
this.value = value;
}
public int getValue() {
return value;
}
public String toString() {
return String.valueOf(value);
}
public static ShowInfoLookup getInstance(int value) {
for (ShowInfoLookup type : ShowInfoLookup.values()) {
if (value == type.value) {
return type;
}
}
return Invalid;
}
}
---我有Hibernate Entity类,我知道EnumType.ORDINAL在这里工作
public class Item {
@Column(name ="ShowAnalystInfo", nullable = false)
@Enumerated(EnumType.ORDINAL)
private ShowInfoLookup showInfo;
}