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.

Note
The error message generated by HibernateTemplate is really misleading !!!

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

avatar
7 Comment threads
3 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
8 Comment authors
AmmadPradeepsambaSubbark Recent comment authors
newest oldest most voted
Ammad
Guest
Ammad

How about if we need two or more parameters in where clause?

Pradeep
Guest
Pradeep

Thanks a lot mkyong!

Subba
Guest
Subba

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.

rk
Guest
rk

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

trackback
Hibernate Tutorials | Tutorials

[…] Remember that ordinal parameters are 1-based! – Hibernate Template […]

afon
Guest
afon

This is because for using named parameters you need to use getHibernateTemplate().findByNamedParam(…) :) instead of simple gHT.find(…)

Kevin
Guest
Kevin

Worked for me, thanks!