Search code examples
javasqlspringjpapredicate

Predicate to retrieve data


I have created a method using criteriaBuilder to return a specific data. Its successful when I run it but return an empty list. Below is the code

public List<WeekCycleEntity> searchOnWeekCycles(String keyword) {

EntityManager em = createEntityManager();
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<WeekCycleEntity> cq = cb.createQuery(WeekCycleEntity.class);



Root<WeekCycleEntity> weekCycleEntity = cq.from(WeekCycleEntity.class);


Predicate findName = cb.like(weekCycleEntity.get("name"), "%" + keyword + "%");
Predicate findExecutedBy = cb.like(weekCycleEntity.get("executedBy"), "%" + keyword + "%");
cq.where(findName, findExecutedBy);



TypedQuery<weekCycleEntity> query = em.createQuery(cq);
return query.getResultList();
}

Solution

  • I believe the only reason could be case not matching so please try this

    cb.like(cb.lower(weekCycleEntity.get("name")), "%" + keyword.toLowerCase() + "%");