Named Query with like in where clause

戏子无情 提交于 2019-11-29 01:46:59

问题


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


回答1:


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 + "%");



回答2:


You can also use CONCAT function

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



回答3:


@Query("select c from Curso c where c.descripcion like CONCAT(:descripcion,'%')")

List<Curso> findByDescripcionIgnoreCase(@Param("descripcion") String descripcion);


来源:https://stackoverflow.com/questions/10912320/named-query-with-like-in-where-clause

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!