Enum as Hibernate Parameter Namedquery

Asked

Viewed 952 times

2

Is there any way to pass as a parameter in a NamedQuery the value of Enum.

public enum TipoConta{
   CREDITO("Cartao de Credito"), DEBITO("Cartao de Debito");
   private final String descricao;

   TipoConta(String descricao) {
    this.descricao = descricao;
   }

   @Query("SELECT g FROM Gasto g WHERE g.transacao.tipo.nome = TipoConta.DEBITO.descricao")
   List<Gasto> findAllGastos();
}

This is an example of what I would like to do, in my code I need to pass as a condition, and I pass a description. But I wouldn’t want to leave like hard code in Query, and pass a constant of a Enum, so I just change the Enum and changes in all Query.

  • In the bank is "Credit card" and "Debit card"?

  • I tried to create an example of my problem because in the code is something else. But what I wanted to do is to pass as a condition the description of a ENUM, Like @Fernando passed, but that way error occurs in Eclipse.

2 answers

4


You can pass parameters to a named query the same way it passes to any JPA query.

Using standard JPA, prepare the query to receive the parameter:

@NamedQuery(
    name="findAllGastos",
    queryString="SELECT g FROM Gasto g WHERE g.transacao.tipo.nome = :tipoConta")

And to consume it:

Query query = em.createNamedQuery("findAllGastos");
query.setParameter("tipoConta", TipoConta.DEBITO.descricao);

And using Spring (your case):

@Query("SELECT g FROM Gasto g WHERE g.transacao.tipo.nome = :tipoConta")
List<Gasto> findAllGastos(@Param("tipoConta") String tipoConta);

Maybe these parameter names aren’t legal because where we’re saying "type" we are actually wanting to receive the "description of the account type"; and we are comparing this "description" with a "name" (Expense.transacao.type.name). Consider refactoring this.

One last tip - don’t post complex business methods on an Enum. Try to use Enum for what it proposes: group constants cohesively and strongly typed.

  • Your solution is very good +1. Only maybe it is a little laborious to it if the parameter of query is fixed, since it would have to every time it runs the query, you will need to remember to always set the parameter as the same value.

  • The problem is I need the description you have on ENUM because in the bank the value in Column is "Credit card".

  • @Danilofernandes You must overwrite the method toString Enum. I updated my answer.

  • I hadn’t thought about the toString of Enum. I just need to perform some tests. Because this ENUM he’s a Enumeration of my Webservice, and the constants are all annotated with @Xmlenum.

  • An error has occurred, it seems that it is not interpreting the toString string. java.lang.Illegalargumentexception: Parameter value [Credit card] Did not match expected type [java.lang.String]

  • @Danilofernandes Then toString from Enum was not invoked to make it compatible with the type of parameter as I expected. The solution is to change the method parameter findAllGastos to String and instead of passing to it TipoConta.DEBITO you have to pass TipoConta.DEBITO.descricao. I updated my answer.

Show 1 more comment

2

I don’t know if I understand you very well.

Change your Enum to constants, because Annotation does not allow receiving instance parameters. As quoted in that and in that issue of the OS.

public enum TipoConta{

    // Troque seu enum para constantes, pois annotation não permite receber parâmetros de instância
    public final static String CREDITO = "Cartao de Credito";
    public final static String DEBITO = "Cartao de Debito";
}

And in query:

// concatene a query a valor do enum (Você também pode parametrizar que é mais adequedo, eficiente e seguro)
@Query("SELECT g FROM Gasto g WHERE g.transacao.tipo.nome = " + TipoConta.DEBITO)
List<Gasto> findAllGastos();
  • He won’t let me do it, show me that mistake: The value for Annotation attribute Query.value must be a Constant Expression. This one of mine @Query is inside an interface.

  • @Danilofernandes Editei as per your comment.

  • @Danilofernandes Will your parameter be static? In other words, this one query you will always do the where column g.transacao.tipo.nome with TipoConta.DEBITO?

Browser other questions tagged

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