Using enums in java

1

I created an enum and need to set the values in the database in this way:

  • If it is Revenue, the value is 0;
  • If Expense, the value is 1;

This is my Enum:

 public enum EntryType {
   INCOME, OUTPUT;
 }

In my java controller, the type comes as String, as I'm getting the value of a radio button:

  <input type="radio" name="income" id="radio-income" value="INCOME"
  ng-model="entry.type">
  <label for="radio-income"><span></span>Entrada</label>

  <input type="radio" name="output" id="radio-output" value="OUTPUT" 
  ng-model="entry.type">
  <label for="radio-output"><span></span>Saída</label>

This is the definition of the EntryType in my entity:

@Enumerated(EnumType.STRING)  // Ja tentei com EnumType.Ordinal
@Column(name = "type")
private EntryType type;

How do I get the ordinal value of the enum to set in the bank, instead of setting the String?

    
asked by anonymous 21.02.2017 / 01:05

1 answer

1

I do not recommend using the ordinal of enum as an identifier of it in the database. If you do this, adding a new element in the non-ending enumeration or rearranging them will generate an inconsistency in your database since the ordinal will change. Example:

Suppose you have the following enumeration:

public enum User Role {

    REGISTERED_USER, ADMIN;
}

In this case, in your database you would have users with the REGISTERED_USER privilege with the value 0 and ADMIN with the value 1. Now suppose you add a new UserRole :

public enum UserRole {

    REGISTERED_USER, GUEST, ADMIN;
}

From now, REGISTERED_USER has the value 0, GUEST has the value 1 and ADMIN the value 2. That is, all ADMIN have become GUEST !

Solution:

What you can do is assign a id to each element and implement a AttributeConverter . That way, with every new element you add, you just have to assign a new identifier that does not have to be sequential.

EntryType.java

public enum EntryType {
    INCOME(0), 

    OUTPUT(1);

    //para uso interno
    //evita que arrays sejam criados desnecessariamente
    private static final EntryType VALUES[] = EntryType.values();

    private final int identificador;

    private EntryType(int identificador) {
        this.identificador = identificador;
    }

    public final int getIdentificador() {
        return identificador;
    }

    //Caso queira, pode retornar um optional ao invés de lançar um exceção
    public static final EntryType deIdentificador(int identificador) {
        for(EntryType et : VALUES) {
            if(et.identificador == identificador) {
                return et;
            }
        }
        throw new IllegalArgumentException("identificador inválido: " + identificador);
    }
}

EntryTypeConverter

public final class EntryTypeConverter implements AttributeConverter<EntryType, Integer> {

    @Override
    public final Integer convertToDatabaseColumn(EntryType attribute) {
        return attribute.getIdentificador();
    }

    @Override
    public final EntryType convertToEntityAttribute(Integer dbData) {
        return EntryType.deIdentificador(dbData);
    }

}

In your organization:

@Column(name = "type")
@Convert(converter=EntryTypeConverter.class)
private EntryType type;
    
21.02.2017 / 01:30