在where子句中使用like命名查询

在where子句中使用like命名查询

问题描述:

是否可以在命名查询的where子句中使用like?我正在尝试执行以下操作但遇到异常

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

我尝试添加%,就像在普通SQL中一样,但得到异常编译。

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

任何指针都非常感谢!

谢谢

你不能在 NamedQuery 中拥有%,但你可以在赋值参数的值中得到它。

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

如:

String city = "needle";
query.setParamter("city", "%" + city + "%");