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