How to write a like query in HQL

Ni3 picture Ni3 · Nov 26, 2012 · Viewed 71k times · Source

I want to perform search for a particular string that is starting with a particular alphabet. So, for example if the starting alphabet is 'A' den it should produce a result which will contain all the strings with alphabet 'A'.

How do I achieve this ?

My query is as shown below

Query qry = session.createQuery("From RegistrationBean as rb where rb."+searchCriteria+"  like %?%");
qry.setString(0,searchField);

Answer

Aleksandr M picture Aleksandr M · Nov 26, 2012

Change your query to this:

Query qry = session.createQuery("From RegistrationBean as rb where rb."+searchCriteria+"  like ?");
qry.setString(0, "%"+searchField+"%");