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 + '%');

29 Comments

  1. miluch says:

    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/

  2. David Atkins says:

    Thanks the blog, found it using google and it solved my weird ‘%’ related problem

  3. Tom says:

    EXCELLENT BLOG!!!

    Thanks for sharing this. It solved my % problem too.

    Regards,
    Tom

  4. html says:

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

  5. Jot says:

    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%

  6. Jot says:

    *String temp = “%”+var+”%”;

  7. auratus says:

    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. :)

  8. Steve says:

    Thanks, this helped me too I couldn’t find it anywhere!

  9. +1 on a big thank you… this confirmed I haven’t completely lost my marbles

  10. Jacob Coens says:

    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.

  11. Chetan says:

    Thanks it solved my problem : String temp = “%”+var+”%”;

  12. kenneth Aqua says:

    simple and concise, if unknown can hold you up quite a bit .. many thanks

  13. flagger says:

    This works as well:

    String query = “from user u where u.name like ‘%’ || :name || ‘%’”

    getHibernateTemplate().findByNamedParam(query, “name”, str);

  14. denny says:

    +1 thank you very much for the tip! I would have lost a lot of time on this!

  15. Sverre says:

    Thx. You saved me a lot of time with this one.

  16. Vishal says:

    Awesome brother helped me a lot

  17. Boris says:

    Thank you!

  18. belano says:

    thanks!

  19. calinutz says:

    i should thank you 2. i used *String temp = “%”+var+”%”;
    works like a charm. very simple

  20. venkat says:

    It worked. Thanks

  21. sugra says:

    I’ve followed all the mental process described in this article (including comments)

    Thanks for make me feel as the rest of programmers :)

  22. rotaku says:

    Thanks you save me!

  23. Steven says:

    Thanks, exactly what I was looking for.

  24. mascagranza says:

    quite usefull mf!!!!

  25. Diederick says:

    Excellent. Works with TopLink Essentials as well.

  26. Aditya says:

    Thanks Stefan….

  27. sam says:

    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.

  28. Manish says:

    Thanks a lot it works!!

  29. [...] a little help from Stefan Pettersson’s blog I found my solution, add the percent signs inside the [...]

Leave a Reply