The code is as follow:
sql = " select '--', staff_no from staff where staff_no = ?";
Session session2 = HibernateUtil.getCurrentSession();
Query sqlQuery2 = session2.createSQLQuery(sql);
sqlQuery2.setParameter(0, "04415"); //error in this line
When run, error
Caused by: org.hibernate.QueryParameterException: Position beyond number of declared ordinal parameters. Remember that ordinal parameters are 1-based! Position: 1
is thrown, I know this is caused by the ‘–’ in sql string, hibernate interpret it as sql comment!!
If I run
sql = " select '-', staff_no from staff where staff_no = ?";
no problem is found.
So how to escape the ‘–’ in hibernate createSqlQuery
?
I try to use
sql = " select \"--\", staff_no from staff where staff_no = ?";
sql = " select \\'--\\', staff_no from staff where staff_no = ?";
sql = " select \'--\', staff_no from staff where staff_no = ?";
but all still thrown the same error.
org.hibernate.QueryParameterException: Position beyond number of declared ordinal parameters. Remember that ordinal parameters are 1-based! Position: 1