I get asked almost every day never how to do a LIKE in an HQL query with parameters.
I want to change this
string hql = "FROM Table t WHERE t.Name=:nameParameter";
into this
string hql = "FROM Table t WHERE t.Name LIKE %:nameParameter%";
It isn’t that simple, this will throw a QueryException.
With a little help from Stefan Pettersson’s blog I found my solution, add the percent signs inside the parameter.
string hql = "FROM Table t WHERE t.Name LIKE %:nameParameter%";
query.SetParameter("nameParameter", "%" + nameParameter + "%");
Advertisement