19

I'm getting Unknown name value for enum class when trying to retrieve records from DB. Using JSF 2.0, JPA.

The possible values in my DB are 'F' or 'J'

Enum:

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 the records from DB I got the error

Would you please help me on this issue? thanks

Stack trace:

javax.servlet.ServletException: Unknown name value for enum 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 for enum class br.com.aaa.xxx.entidade.TipoPessoa: F .... ......

Sahan Serasinghe
  • 1,591
  • 20
  • 33
Al2x
  • 1,001
  • 5
  • 26
  • 37
  • You are doing the mapping wrong. How should Hibernate know how to map the enum type? See e.g. [this SO question](http://stackoverflow.com/questions/417062/enumerations-in-hibernate). – Dominik Sandjaja Jul 05 '13 at 17:03
  • @surfealokesea stacktrace updated in the question. – Al2x Jul 05 '13 at 17:11

1 Answers1

24

Hibernate doesn't know and care about the id field inside your enum. All it knows about is the ordinal value (0 and 1) and the name (FISICA and JURIDICA). If you want to persist F and J, you'll have to rename your two enum constants to F and J, and annotate the field in the entity like this:

@Column(nullable=false, length=1)
@Enumerated(EnumType.STRING)
private TipoPessoa tipoPessoa;

or use a custom user type to transform F to FISICA or vice-versa.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • Thats great. Worked as expected. But How can I do to let the full names instead F or J ? – Al2x Jul 05 '13 at 17:16
  • 2
    Use a custom user type: http://docs.jboss.org/hibernate/orm/4.2/manual/en-US/html_single/#types-custom – JB Nizet Jul 05 '13 at 17:19
  • **RESOLVED*** @JB Nizet thank you so much for helping. Im newly in Java and spent some hours trying to fix that. Now its fixed by you :) – Al2x Jul 05 '13 at 17:21
  • @JB Nizet when I remove enum string in java. but it is in DB. when I retrieve the records from the DB. It gives a null pointer exception. How to overcome this problem? – Kumaresan Perumal Feb 28 '20 at 12:47
  • @KumaresanPerumal so you have a value in the database that is supposed to be mapped to an enum, but doesn't have any of the valid enum values? Don't do that. Change the values in the DB to a valid enum value, or leave the enum as it was. – JB Nizet Feb 28 '20 at 15:42