简体   繁体   English

枚举与查询表

[英]Enum versus lookup table

I'm implementing a solution in Java using Ebean and I'm having some issues choosing between using Enums or simply look up tables. 我正在使用Ebean在Java中实现一个解决方案,并且在使用Enums或只是查找表之间进行选择时遇到了一些问题。

I have a table "Tooth". 我有一张桌子“牙齿”。 A tooth can be "Temporary" or "Permanent". 牙齿可以是“临时”或“永久”的。

I could create a simple Enum: 我可以创建一个简单的枚举:

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

However if I want to do a direct SQL query I have to convert "T" and "P", so a solution would be to use a lookup table as below: 但是,如果要执行直接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;
    }
}

This feels kind of hardcoded and for larger tables a lot of isSomething functions are required. 这感觉有点硬编码,对于较大的表,需要很多isSomething函数。

Is there a better solution? 有更好的解决方案吗? Thanks in advance. 提前致谢。

Why do you not use? 为什么不使用?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM