Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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..

like image 604
Java Developer Avatar asked Jan 02 '13 09:01

Java Developer


1 Answers

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.

like image 195
Subin Sebastian Avatar answered Oct 04 '22 23:10

Subin Sebastian