Uppercase in Criteriabuilder

Asked

Viewed 145 times

0

I have the following consultation with Criteriabuilder

    CriteriaBuilder cb = manager.getCriteriaBuilder();
    CriteriaQuery<Escola> cq = cb.createQuery(Escola.class);

    Root<Escola> root = cq.from(Escola.class);
    cq.select(root);
    List<Predicate> filtros = new ArrayList<>();

    if(filtro!=null) {
        if(filtro.getCnpj() != null && !filtro.getCnpj().isEmpty() ) {
            Path<String> campoCnpj = root.<String>get("cnpj");
            Predicate filtroCnpj =  cb.like(campoCnpj, "%"+filtro.getCnpj()+"%" ) ;  //(Predicate) cb.like(campoRazaoSocial , "%"+filtro.getRazaoSocial().toLowerCase()+"%"); 
            filtros.add(filtroCnpj) ;
        }

        if(filtro.getRazaoSocial() != null && !filtro.getRazaoSocial().isEmpty() ) {
            Path<String> campoRazaoSocial = root.<String>get("razaoSocial");
            Predicate filtroRazaoSocial =  cb.like(campoRazaoSocial, "%"+filtro.getRazaoSocial()+"%" ) ;  //(Predicate) cb.like(campoRazaoSocial , "%"+filtro.getRazaoSocial().toLowerCase()+"%"); 
            filtros.add(filtroRazaoSocial) ;
        }

        cq.where(filtros.toArray(new Predicate[0]));
    }



    TypedQuery<Escola> tq = manager.createQuery(cq);
    return tq.getResultList();

It works, but I wish the filter didn’t differentiate between upper and lower case.

On the Predicate line (below) I can even place toUpperCase in the filter field. But how do I do the same in the Path field?

Path<String> campoRazaoSocial = root.<String>get("razaoSocial");
Predicate filtroRazaoSocial =  cb.like(campoRazaoSocial, "%"+filtro.getRazaoSocial()+"%" ) ;  //(Predicate) cb.like(campoRazaoSocial , "%"+filtro.getRazaoSocial().toUpperCase()+"%");

1 answer

0


To whom it may concern

to do the upper just do as in the example below.

Without the uppercase

Predicate filtroRazaoSocial =  cb.like(campoRazaoSocial, "%"+filtro.getRazaoSocial()+"%" ) ;

Like the uppercase

Predicate filtroRazaoSocial =  cb.like(cb.upper(campoRazaoSocial), "%"+filtro.getRazaoSocial().toUpperCase()+"%" )

Browser other questions tagged

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