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()+"%");