Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Named Query with like in where clause

Is it possible to have a like in a where clause in a named query? I am trying to do the following but am getting exceptions

@NamedQuery(name = "Place.getPlaceForCityAndCountryName", query = "SELECT p FROM Place p WHERE " +         "lower(p.city) like :city and " +         "lower(p.countryName) like :countryName"); 

I tried adding % as you would do in normal SQL but get exceptions compiling.

Any pointers greatly appreciated!

Thanks

like image 338
RNJ Avatar asked Jun 06 '12 10:06

RNJ


1 Answers

You can't have the % in the NamedQuery, but you can have it in the value you assign the parameter.

As in:

String city = "needle"; query.setParamter("city", "%" + city + "%"); 
like image 130
esej Avatar answered Sep 25 '22 17:09

esej