[英]Hibernate could not locate named parameter even if it exist
Hibernate保持檢測
org.hibernate.QueryParameterException: could not locate named parameter [name]
即使它存在。 這是我的hql
Query query = sess().createQuery("from UserProfile where firstName LIKE '%:name%'").setParameter("name", name);
為什么hibernate不斷拋出異常? 即使參數存在?
應該是這樣的:
Query query = sess().createQuery("from UserProfile where firstName LIKE :name")
.setParameter("name", "%"+name+"%");
在你的情況下':name'
是Hibernate將搜索的實際字符串。 如果你需要一個真正的命名參數,你需要只有:name
。
因此%
應作為值傳遞:name
並且Hibernate將替換:name
with actual value。
請注意,如果您的值包含%
並且您希望它是實際的字母而不是通配符,則必須將其轉義, 這是 escaper-class的示例。
嘗試使用hql
連接它
"from UserProfile where firstName LIKE '%' || :name || '%'"
或使用CONCAT
"from UserProfile where firstName LIKE CONCAT('%', :name ,'%')"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.