I don’t recommend using the ordinal enum
as her identifier in the database. If you do this, adding a new element in the enumeration that is not at the end or reordering 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 privilege REGISTERED_USER
with the value 0 and ADMIN
value 1. Now suppose you add a new UserRole
:
public enum UserRole {
REGISTERED_USER, GUEST, ADMIN;
}
From now on, REGISTERED_USER
has a value of 0, GUEST
has a value of 1 and ADMIN
the value 2. That is, all ADMIN
became GUEST
!
Solution:
What you can do is assign a id
to each element and implement a AttributeConverter
. That way, for every new element you add, just assign a new identifier that doesn’t need 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 its entity:
@Column(name = "type")
@Convert(converter=EntryTypeConverter.class)
private EntryType type;
There is no error, I only need to set 0 or 1 and currently Seto as INCOME or OUTPUT.
– Guilherme Nass