2014-09-10 86 views
0

我正在使用Ebean在Java中实现一个解决方案,我在使用枚举或简单查找表格之间进行了一些选择。枚举与查找表

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

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

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

这种感觉硬编码和大表很多isSomething功能是必需的。

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

+0

我在同一条船上,我试图找出指定参考/查找表的最佳方法。我所有的查询都来自数据库,这样我可以保持参照完整性。你最终做了什么? – dotnetster 2014-12-05 18:12:34

+0

@dotnetster我使用了查找表。但我仍然觉得硬编码... – KirdApe 2014-12-09 09:58:35

回答

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

好的构造函数:) – panagdu 2014-09-10 09:30:42