5
I have the following field classificacao
in a table.
This field is filled in elsewhere and is filled with, only, 'P'
or 'N'
, acronyms for Positive and Negative.
I then created the enum
Java.
Classificacao.
public enum Classificacao {
POSITIVO('P'), NEGATIVO('N');
private char classificacao;
Classificacao(char classificacao) {
this.classificacao = classificacao;
}
public char getClassificacao() {
return classificacao;
}
}
In my entity, I have the attribute:
@Enumerated(EnumType.STRING)
@Convert(converter = ClassificacaoEnumConverter.class)
@Column(name = "classificacao", length = 1)
private Classificacao classificacao;
When I try to list this table, the following Exception is launched:
java.lang.IllegalArgumentException: Unknown name value [N] for enum class [br.com.jpalab.enums.Classificacao]
I understand why, from his mistake, he seeks the value N
, where there is only POSTIVO
and NEGATIVO
, but how could I solve this problem?
I created this Converter
@Converter
public class ClassificacaoEnumConverter implements AttributeConverter<Classificacao, String> {
@Override
public String convertToDatabaseColumn(Classificacao classificacao) {
return String.valueOf(classificacao.getClassificacao());
}
@Override
public Classificacao convertToEntityAttribute(String classificacaoFromDb) {
if (classificacao == null) return null;
switch (classificacaoFromDb) {
case "P":
return Classificacao.POSITIVO;
case "N":
return Classificacao.NEGATIVO;
}
throw new IllegalStateException();
}
}
Without converting it shouldn’t be enough? At least for JPA is, iirc.
– Gustavo Cinque
@Gustavocinque Hence the
name()
of each element ofenum
would have to be persisted. That is, instead of just beingP
andN
in the database, it would have to bePOSITIVO
andNEGATIVO
.– Victor Stafusa
True, only after I commented that I realized there was a difference in what the author wanted as Enum’s name and what was going to the bank.
– Gustavo Cinque
I already tried to use Converter, it keeps launching the same Exception
– Henrique Santiago
It was faster than me :). Alternatively we can create a table
CLASSIFICACAO
withID
andNOME
. Then we can create a FK in the table that uses the Enum referencingCLASSIFICACAO.CLASSIFICAO_ID
and map with@Column(name="CLASSIFICAO_ID") 
@Enumerated(EnumType.ORDINAL)
. The advantage is that if the Enum change in some way (e.g., if you rename or add values) is much easier to make the change, although it creates problems if you reorder the members of Enum only on the Java side.– Anthony Accioly
@Henriquesantiago tried the
@Converter(autoApply = true)
?– Victor Stafusa
@Victorstafusa yes, inclusive, I put a log and debugged, the methods of the interface Attribute Converter are not even being called.
– Henrique Santiago
the
autoapply = true
is for when you want to do this for all attributes that have Rating, even setting the convert in the @Convert annotation, IE, being redundant, does not work– Henrique Santiago
@Henriquesantiago What is the version of your JPA? And Hibernate?
– Victor Stafusa
<hibernate-entitymanager.version>5.2.10.Final</hibernate-entitymanager.version>
– Henrique Santiago
I only have this dependency, I have no dependency on jpa
– Henrique Santiago
I took the note of
@Enumerated
and it worked.– Henrique Santiago
--' :( :D --' ( :D
– Henrique Santiago
@Henriquesantiago KKKKK - That is, it has to be Converter without Enumerated.
– Victor Stafusa
@Henriquesantiago Reply edited.
– Victor Stafusa
exact, puts! kkkkkkk
– Henrique Santiago