Java – hibernate exception: unknown name value of enumeration class
When I try to retrieve a record from the DB, I get the unknown name value of the enumeration class Using JSF 2.0, JPA
The possible values in my dB are 'f' or 'J'
Enumeration:
public enum TipoPessoa { FISICA ("F","Física"),JURIDICA ("J","Jurídica"); private final String id; private final String descricao; private TipoPessoa(String id,String descricao){ this.id = id; this.descricao = descricao; } public String getId() { return id; } public String getDescricao(){ return descricao; } }
Entity:
@Column(nullable=false,length=1) private TipoPessoa tipoPessoa; public TipoPessoa getTipoPessoa() { return tipoPessoa; } public void setTipoPessoa(TipoPessoa tipoPessoa) { this.tipoPessoa = tipoPessoa; }
When I try to read records from DB, I receive an error
Can you help me with this question? Thank you
Stack trace:
javax. servlet. ServletException: unknown name value of enumeration class.br com. aaa. xxx. entidade. TipoPessoa:F javax. faces. webapp. FacesServlet. service(FacesServlet.java:606) br. com. aaa. filtro. FiltroEncode. Dofilter (filtroencode. Java: 26) root cause
javax. ejb. Ejbtransactionrolledbackexception: unknown name value of enumeration Classbr com. aaa. xxx. entidade. TipoPessoa:F …. ……
Solution
Hibernate doesn't know and cares about the ID field in your enumeration All it knows are ordinal values (0 and 1) and names (Fisica and juridica) If you want to keep F and j, you must rename the two enumeration constants to f and j and annotate the fields in the entity:
@Column(nullable=false,length=1) @Enumerated(EnumType.STRING) private TipoPessoa tipoPessoa;
Or use a custom user type to convert f to Fisica, and vice versa