How to create a date search method?

Asked

Viewed 43 times

2

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

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

In my code it’s like this:

Java client.:

Calendar dtNascimento; ...gets e sets

Clientedaoimpl:

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

Clientele:

public List<Cliente> buscaNascimento(Calendar dtNascimento);

Clientebean:

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);
}

Page XHTML (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"/>

1 answer

1

I got this reply from a friend and solved my problem!

In case anyone is in trouble, follow:

First point, the like is used to buy String, so it doesn’t work very well with dates. Second, how is the attribute mapped dtNascimento in your Customer class? Be sure to use the @Temporal(TemporalType.XXX) in this attribute for JPA not to be lost.

Finally, use the following jpql:

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

or simply

FROM Cliente c WHERE c.dtNascimento = :param;

Browser other questions tagged

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