Search code examples
javahibernatejpahql

Hibernate Named Query Using Like and % % operators?


In my Hibernate JPA Sample code..

public List<AttendeesVO> addAttendees(String searchKeyword) {
    TypedQuery<AttendeesVO> query = entityManager.createQuery(" select at from AttendeesVO at where at.user.firstName LIKE :searchKeyword",AttendeesVO.class);
    query.setParameter("searchKeyword", searchKeyword+"%");
    return query.getResultList();
}

it is working fine when giving entire String firstName=Narasimham

But it is not working when we give any character of Narasimham i.e a or n

Actually my thinking is i'm giving Like operator with % % so it working any character of given String..


Solution

  • you are using query.setParameter("searchKeyword", searchKeyword+"%");

    instead of query.setParameter("searchKeyword", "%"+searchKeyword+"%");

    first one will return rows for Narasimham N Na Nar Nara etc.