How to create a date search method?

2

I can not in any way create a date search method that works.

I have already been able to register the date and the other attributes as well. I've already been able to list all the attributes and also search all attributes quietly, minus the date.

In my code it looks like this:

Client.java :

Calendar dtNascimento; ...gets e sets

IMO Client :

@Override
public List<Cliente> buscaNascimento(Calendar dtNascimento) {
    return em.createQuery("from Cliente c where "
            + "c.dtNascimento like :param", Cliente.class)
            .setParameter("param", dtNascimento)
            .getResultList();   
}

ODD Client :

public List<Cliente> buscaNascimento(Calendar dtNascimento);

ClientBean :

public void buscarDtNascimento(){
    FacesMessage msg;
    List<Cliente> encontrados = cliDao.buscaNascimento(cli.getDtNascimento());
    if(encontrados != null){
        for(@SuppressWarnings("unused") Cliente c : encontrados){

        }
        lista = encontrados;
        msg =  new FacesMessage("Dados Encontrados");
    }else{
        msg =  new FacesMessage("Dados Incorretos");
    }
    FacesContext.getCurrentInstance().addMessage(null, msg);
}

XHTML Page (Primefaces):

<p:outputLabel id="dtNascimento" value="Data de Nascimento" />
                <p:calendar for="dtNascimento" mask="99/99/9999" pattern="dd/MM/yyyy" value="#{clienteBean.cli.dtNascimento.time}"/>
                <p:commandButton icon="ui-icon-search" action="#{clienteBean.buscarDtNascimento}" update="lista"/>
    
asked by anonymous 28.09.2017 / 21:48

1 answer

1

I received this response from a friend and solved my problem!

If someone has this problem, it follows:

  

First point, like is used for String purchase, so it does not work very well with dates.   Second, how is the dtNascimento attribute in your Client class mapped? Make sure you use @Temporal(TemporalType.XXX) in this attribute so JPA is not lost.

     

Finally, use the following jpql:

SELECT c FROM Cliente c WHERE c.dtNascimento = :param;
     

or simply

FROM Cliente c WHERE c.dtNascimento = :param;
    
29.09.2017 / 01:05