UpperCase in CriteriaBuilder

0

I have the following query 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 would like the filter to not be case-sensitive.

In the Predicate line (below) I can even put the 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()+"%");
    
asked by anonymous 11.10.2017 / 13:37

1 answer

0

Who can interest

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

Without upperCase

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

As upperCase

Predicate filtroRazaoSocial =  cb.like(cb.upper(campoRazaoSocial), "%"+filtro.getRazaoSocial().toUpperCase()+"%" )
    
16.10.2017 / 21:03