Remember that ordinal parameters are 1-based! – HibernateTemplate
Problem
HibernateTemplate code …
getHibernateTemplate().find("from Domain d
where d.domainName = :domainName", domainName);
When i execute the above code, i hit the following error message
java.lang.IndexOutOfBoundsException: Remember that ordinal parameters are 1-based!
...
at org.hibernate.impl.AbstractQueryImpl.determineType(AbstractQueryImpl.java:397)
at org.hibernate.impl.AbstractQueryImpl.setParameter(AbstractQueryImpl.java:369)
Solution
I go inside and study HibernateTemplate.java file and find below code
public List find(final String queryString, final Object[] values) throws DataAccessException {
return (List) executeWithNativeSession(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException {
Query queryObject = session.createQuery(queryString);
prepareQuery(queryObject);
if (values != null) {
for (int i = 0; i < values.length; i++) {
queryObject.setParameter(i, values[i]);
}
}
return queryObject.list();
}
});
}
From code above, the HibernateTemplete is using 0-based instead of 1-based. Is this a spring or hibernate library problem? Since error message stated parameters need to start at 1-based. I tried some solution like change spring or hibernate library, however it’s not working…
It’s seem I’m on a wrong direction, i have to start finding solution at beginning again, first i study my own code…………!!! I cant imaging how careless i am, i made a stupid mistake on my code, this is not spring or hibernate problem, it is my syntax error.
Change from
getHibernateTemplate().find("
from Domain d where d.domainName = :domainName", domainName);
To
getHibernateTemplate().find("
from Domain d where d.domainName = ?", domainName);
Problem solved, code execute without error anymore.
The error message generated by HibernateTemplate is really misleading !!!
How about if we need two or more parameters in where clause?
not ending there, if you have a statelessSession.createQuery(“from com.abc.cde.SomeEntity entity where entity.someProc = :someParam”).setParameter(“someParam”, 1) will throw the same exception if the bean is not defined in your session factory/configuration.
This is even more confusing
Hi rk,
Thanks for sharing your experience. Hope Hibernate can throw more meaningful error message in future.
Thanks a lot mkyong!
Thanks for this blog. i had come across the same problem. yes, the error is misleading. But your analysis saved my time. Thanks a lot.
This is because for using named parameters you need to use getHibernateTemplate().findByNamedParam(…) 🙂 instead of simple gHT.find(…)
ya..thanks for your solution.
I got above issue while using hibernate with JPA. It helped me in solving issue. Thank you.
Worked for me, thanks!