枚举与查找表

时间:2014-09-10 08:19:38

标签: java ebean

我正在使用Ebean在Java中实现一个解决方案,我在使用Enums或只是查找表时遇到了一些问题。

我有一张桌子“Tooth”。牙齿可以是“临时”或“永久”。

我可以创建一个简单的枚举:

@EnumMapping(nameValuePairs = "TEMPORARY=T, PERMANENT=P")
public enum DentitionType { TEMPORARY, PERMANENT; }

但是如果我想进行直接的SQL查询,我必须转换“T”和“P”,所以解决方案是使用如下的查找表:

@Entity
public class DentitionType {

    @Column(length = 15)
    public String name;

    private static DentitionType permanent;

    public boolean isTemporary() {
        return !this.equals(getPermanent());
    }

    public boolean isPermanent() {
        return this.equals(getPermanent());
    }

    public static DentitionType getPermanent() {
        if (permanent == null) {
            permanent = DentitionType.FIND.byId(2L);
        }

        return permanent;
    }
}

这种感觉有点硬编码,而对于较大的表格,需要很多功能。

有更好的解决方案吗?提前谢谢。

1 个答案:

答案 0 :(得分:0)

为什么不使用?

public enum DentitionType {
    TEMPORARY('T'), PERMANENT('P');
    private char value;
    private Currency(char value) {
        this.value = value;
    }
    public static DentitionType Get(final char value){
        for (DentitionType type : DentitionType.values())
        if (type.name == name)
            return type;
        return null;
    }
};
相关问题