Enum error with JPA + Hibernate

Asked

Viewed 325 times

2

When performing a query in the database with JPA + HIBERNATE the following exception shall be made:

java.lang.IllegalArgumentException: Unknown name value [Respondido] for enum class [java.lang.Enum]

I cannot understand the reason for the error, in theory it should be making the Binding correctly for the database column.

This is my Enum in @Entity

@Enumerated(EnumType.STRING)
@Column(name = "situacao", nullable = true)
private Enum<SituacaoAvaliacaoPedidoEnum> situacao;

And this is my query query, I’m using the interface SimpleJPARepository.

@Query("SELECT situacao FROM AvaliacaoPedido ap WHERE ap.idPedido = :idPedido")
public String consultarSituacaoPedido(@Param("idPedido") Integer idPedido);

And this is my Enum:

public enum SituacaoAvaliacaoPedidoEnum {

    EsperandoResposta, 
    Respondido, 
    FalhaEnvio, 
    Ignorado, 
    ExcedeuTentativa;
}

I already saw in a question of the GUJ that the problem could be that in the bank the Enum could be in full, but this is not my case, the Num in my bank are all String as you can see below:

inserir a descrição da imagem aqui

  • 1

    Hello @Anthonyaccioly I performed the SELECT DISTINCT in the comic book and there was no value stuck, so I did what you said I changed the private Enum<SituacaoAvaliacaoPedidoEnum> situacao; for private SituacaoAvaliacaoPedidoEnum situacao; and it worked, could you just do me two more favors? 1° - could you put that comment of yours as an answer so I can mark the question with the answer? the 2° is if you would not have some article you recommend and talk about those type Erasure I would like to know more about the subject.

1 answer

5


The problem is that you are using the common superclass Enum:

private Enum<SituacaoAvaliacaoPedidoEnum> situacao;

As opposed to Enum’s direct type SituacaoAvaliacaoPedidoEnum:

private SituacaoAvaliacaoPedidoEnum situacao;

Reading the relevant parts of JSR 338 (JPA 2.2) and of Documentation of Hibernate all examples expect the annotation @Enumerated is used directly with the Enum type.


And why JPA does not accept fields of the type Enum<E>?

I don’t know why. I speculate that this is a more exotic use case that they simply haven’t implemented.

Enum is a common base class for all enumeration types. Despite the use of Enum as a field of a class being unusual, the code below is perfectly valid:

private Enum<SituacaoAvaliacaoPedidoEnum> situacao = SituacaoAvaliacaoPedidoEnum.EsperandoResposta;

My initial suspicion was that due to Type Erasure the generic type would not be accessible at runtime, however, this is not true. With the help of Reflection I was able to get the guy E in a Enum<E> and initialize the field normally through a String.

Watch it work on Ideone.com

Of course there are more complicated cases (e. g., Wildcards in scenarios such as private Enum<? extends SituacaoAvaliacaoPedidoEnum> situacao;, generic types coming from the class, etc). It is possible that in some of the most complicated cases it is not really possible to get the type of Enum; most likely however this simply has not been implemented.

With Hibernate it is always possible to implement a Basic Type of Custom to circumvent the limitation; that being said, I believe that in most cases using the direct Enum type is a cleaner solution.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.