Hibernate HQL like query using named parameters
Using the LIKE condition with a “%” sign for pattern matching in a HQL query with named parameters required some tweaking.
This did not work:
String query = "from user u where u.name like %:name%" getHibernateTemplate().findByNamedParam(query, "name", str);
It resulted in a org.hibernate.QueryException: unexpected char: ‘%’ error.
Next I tried using single quotes:
String query = "from user u where u.name like '%:name%'" getHibernateTemplate().findByNamedParam(query, "name", str);
Didn’t work either, an org.hibernate.QueryParameterException: could not locate named parameter.
What actually worked was putting the “%” signs in the parameter:
String query = "from user u where u.name like :name" getHibernateTemplate().findByNamedParam(query, "name", '%' + str + '%');
Hi
I am wondering why not getting rid of hibernate template and use hibernate session directly. Take a look at
http://blog.springsource.com/main/2007/06/26/so-should-you-still-use-springs-hibernatetemplate-andor-jpatemplate/
Thanks the blog, found it using google and it solved my weird ‘%’ related problem
EXCELLENT BLOG!!!
Thanks for sharing this. It solved my % problem too.
Regards,
Tom
Thanks for sharing. But it cannot solve the problem with value searching is “%”.
nameValue = “%”;
String query = “from user u where u.name like ‘%:name%’”
getHibernateTemplate().findByNamedParam(query, “name”, nameValue);
An even simpler way:
create a new string with the % around the string you want to insert into the query:..
String temp = “%”+title+”%”;
This will hold as %var%
Then send that string TEMP to the HQL query…
Query query = session.createQuery(“from Event e where e.title like ?” );
query.setString(0, temp);
therefore it will interpret as
from Event e where e.title like %var%
*String temp = “%”+var+”%”;
Thanks for the blog entry, helped me out too – in my case, the comment about adding “%” to the var instead of the original String is what fixed it.
Thanks, this helped me too I couldn’t find it anywhere!
+1 on a big thank you… this confirmed I haven’t completely lost my marbles
Very helpful, thanks! I’m using Castle ActiveRecord and it’s sometimes hard to find out how to use certain features using the provided documentation only.
Thanks it solved my problem : String temp = “%”+var+”%”;
simple and concise, if unknown can hold you up quite a bit .. many thanks
This works as well:
String query = “from user u where u.name like ‘%’ || :name || ‘%’”
getHibernateTemplate().findByNamedParam(query, “name”, str);
+1 thank you very much for the tip! I would have lost a lot of time on this!
Thx. You saved me a lot of time with this one.
Awesome brother helped me a lot
Thank you!
thanks!
i should thank you 2. i used *String temp = “%”+var+”%”;
works like a charm. very simple
It worked. Thanks
I’ve followed all the mental process described in this article (including comments)
Thanks for make me feel as the rest of programmers
Thanks you save me!
Thanks, exactly what I was looking for.
quite usefull mf!!!!
Excellent. Works with TopLink Essentials as well.
Thanks Stefan….
session.createQuery(“from Entity where property like :prefix”).setString(“prefix”,myString.concat(“%”)).list();
solved my problem, but actually i was searching for a better solution …
looks quite dirty to me.
Thanks a lot it works!!
[...] a little help from Stefan Pettersson’s blog I found my solution, add the percent signs inside the [...]