My name search is not working, somehow it is not passing Predicate
.
When I make a request at: localhost: 8080 / citys? name = 2
You will have the same entities as: localhost: 8080 / citys
Note: my column name
is city 1, city 2 ....
Resource:
@GetMapping
public Page<City> getAllENtity( CityFilter cityFilter, Pageable pageable) {
return repository.filtrar(cityFilter, pageable);
}
Repository:
public interface CityRepository extends JpaRepository<City, Long>, CityRepositoryQuery{}
Query:
public interface CityRepositoryQuery {
public Page<City> filtrar(CityFilter cityFilter, Pageable pageable);
}
Impl:
public class CityRepositoryImpl implements CityRepositoryQuery {
@PersistenceContext
private EntityManager manager;
@Override
public Page<City> filtrar(CityFilter cityFilter, Pageable pageable) {
CriteriaBuilder builder = manager.getCriteriaBuilder();
CriteriaQuery<City> criteria = builder.createQuery(City.class);
Root<City> root = criteria.from(City.class);
Predicate[] predicates = createFilter(cityFilter, builder, root);
criteria.where(predicates);
TypedQuery<City> query = manager.createQuery(criteria);
addPageRestrict(query, pageable);
return new PageImpl<>(query.getResultList(), pageable, total(cityFilter));
}
private Predicate[] createFilter(CityFilter cityFilter, CriteriaBuilder builder, Root<City> root) {
List<Predicate> predicates = new ArrayList<>();
if (!StringUtils.isEmpty(cityFilter.getName())) {
predicates.add(
builder.like(builder.lower(root.get(City_.name)), "%" + cityFilter.getName().toLowerCase() + "%"));
System.out.println("esta pesquisando nome de city");
}
return predicates.toArray(new Predicate[predicates.size()]);
}