2
\$\begingroup\$

I'm implementing a solution in Java using Ebean and I'm having some issues choosing between using enums or simply look up tables.

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:

@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.

Is there a better solution?

\$\endgroup\$
  • \$\begingroup\$ Could you explain a bit more? Not sure what is asked for. Do you want a better version of the enum to have defined enumValue pairs instead of the annotation? \$\endgroup\$ – thepace Dec 9 '14 at 10:55
  • \$\begingroup\$ I would like to have the best of both worlds: have an entity (stored as a table in the db) but with values which have specific meaning. \$\endgroup\$ – KirdApe Dec 9 '14 at 15:18
1
\$\begingroup\$

I would recommend making an enum with properties, such as:

public enum DentitionType {
    TEMPORARY(1), PERMANENT(2);

    private final int id;
    private DetentionType(int id) {
        this.id = id;
    }

    public int getId() {
        return this.id;
    }

    public static DententionType forId(int id) {
        for (DententionType dentention : DetentionType.values()) {
            if (dentention.getId() == id) {
                return dentention;
            }
        }
        return null;
    }
}

This is an easy way of mapping your enum to an int (you can also use a 'char' or 'String' or whatever you feel like). Then you can inside your @Entity class use some JPA events to transform to/from enum and int.

Another solution is of course to simply use the @Enumerated annotation and automatically use the ordinal of the enum. Be aware however that there has been incidents where someone accidentally switched the order of the enums, which caused DIAMONDS to become black.

\$\endgroup\$
1
\$\begingroup\$

With Enum you get these weapons for lookup:
- DentitionType.TEMPORARY.getValue() to get the string "T" or "P".
- DentitionType.valueOf("T") to get the enumType i.e DentitionType.TEMPORARY
- For any new Type just add the type and your job is done.

public class DentitionType{
    TEMPORARY("T"),
    PERMANENT("P"),
    ARTIFICIAL("A"),
    ;

    private String value;
    private DentitionType( String value ){
        this.value = value;
    }
    public getValue(){
        return this.value;
    }
}

Considering the current program, instead of having multiple isXYZ, try

boolean isMatchingType(DentitionType type){ // For permanent use DentitionType.PERMANENT
    return this.name.equals(type.getValue());
}
\$\endgroup\$
  • 1
    \$\begingroup\$ As it is an enum, I would just compare them with this == other, using .equals for enums is not necessary. \$\endgroup\$ – Simon Forsberg Dec 12 '14 at 13:19
  • \$\begingroup\$ But enums are not persisted to their own table in the database. \$\endgroup\$ – KirdApe Dec 15 '14 at 12:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.