user962206
user962206

Reputation: 16117

Hibernate could not locate named parameter even if it exist

Hibernate Keeps detecting

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

even though it exist. here's my hql

Query query = sess().createQuery("from UserProfile where firstName LIKE '%:name%'").setParameter("name", name);

Why does hibernate keeps throwing that exception? even though the parameter exist?

Upvotes: 25

Views: 53868

Answers (2)

Stanislav Bashkyrtsev
Stanislav Bashkyrtsev

Reputation: 15308

Should be like this:

Query query = sess().createQuery("from UserProfile where firstName LIKE :name")
                    .setParameter("name", "%"+name+"%");

In your case ':name' is actual string Hibernate will search for. If you need to have a real named parameter, you need to have just :name.

Thus % should be passed as a value of :name and Hibernate will substitute :name with actual value.

Note, that if your value contains % and you want it to be an actual letter instead of wildcard, you'll have to escape it, here is an example of escaper-class.

Upvotes: 36

John Woo
John Woo

Reputation: 263683

try to concatenate it using hql

"from UserProfile where firstName LIKE '%' || :name || '%'"

or using CONCAT

"from UserProfile where firstName LIKE CONCAT('%', :name ,'%')"

Upvotes: 5

Related Questions