How to specify a JPA named parameter surrounded by

2019-01-17 19:05发布

How would I specify a JPA query like:

Query q = 
  em.createQuery(
    "SELECT x FROM org.SomeTable x WHERE x.someString LIKE '%:someSymbol%'"
  );

followed by:

q.setParameter("someSymbol", "someSubstring");

and not triggering a

org.hibernate.QueryParameterException: could not locate named parameter [id]

Much appreciated!

3条回答
狗以群分
2楼-- · 2019-01-17 19:41

For reference, you could also use CONCAT:

like CONCAT('%', :someSymbol, '%')
查看更多
Root(大扎)
3楼-- · 2019-01-17 19:42

query.setParameter(someSymbol, "%" + someSymbol+ "%");

查看更多
smile是对你的礼貌
4楼-- · 2019-01-17 19:50

How about

Query q = 
  em.createQuery(
    "SELECT x FROM org.SomeTable x WHERE x.someString LIKE :someSymbol"
);
q.setParameter("someSymbol", "%someSubstring%");

I'm pretty sure I once solved your problem like that.

查看更多
登录 后发表回答