How to do this consultation with JPA?

1

I would like two solutions to this question, one using a normal typedquery and another using the criteria because of its great versatility for the code.

Here is my query:

      TypedQuery<Pessoa> query = em.createQuery("SELECT a FROM Pessoa a where  
      a.dataNascimento <> '12/9/2000'", Pessoa.class);

How do I return this query?

Here is the error:

Caused by: java.lang.ClassCastException: java.lang.String cannot be cast to java.time.LocalDate
    
asked by anonymous 18.08.2016 / 15:26

2 answers

2

Try to modify your where

'12 / 9/2000 '"

As the equal "="

would look like this: "a.dataNation = '12 / 9/2000 '"

//////////////////////////

Well, I think the problem revolves around the return of the method that contains your query, right? Well, I think this method should solve by using setParamenter ... kkk

public Pessoa buscaDataNasc(String dataNascimento){
        Pessoa pessoa = this.manager.createQuery("select p from Pessoa p where p.dataNascimento = :pDataNascimento")
                .setParameter("pDataNascimento", dataNascimento).getResultList();

        return pessoa;
    }

ps: I do not have much affinity with the criteria API.

    
18.08.2016 / 16:39
0

It looks like the error is in date format ...

I had a similar error, the problem is in the search for the '12 / 9/2000 'format that is Brazilian format, and the BD usually works with the international that is' 2000-09-12'

Try to search this way ...  "a.dataNation = '2000-09-12'"

    
18.08.2016 / 16:47