Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why SQLQuery setParameter cannot use LIKE?

Tags:

java

hibernate

I have here:

Session session = getSession();
SQLQuery query = session.createSQLQuery("SELECT * FROM PERSON WHERE NAME LIKE '%?%'");
query.setParameter(0, personName);

I get the following error:

java.lang.IndexOutOfBoundsException: Remember that ordinal parameters are 1-based!

But when I try:

Session session = getSession();
SQLQuery query = session.createSQLQuery("SELECT * FROM PERSON WHERE NAME = ?");
query.setParameter(0, personName);

its working.

I need to use LIKE.

like image 881
NinjaBoy Avatar asked Dec 20 '22 18:12

NinjaBoy


1 Answers

You can do like this:

Session session = getSession();
SQLQuery query = session.createSQLQuery("SELECT * FROM PERSON WHERE NAME LIKE ?");
query.setParameter(0, "%" + personName + "%");
like image 186
Keppil Avatar answered Dec 23 '22 08:12

Keppil