Main Tutorials

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 Author

author image
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

Subscribe
Notify of
9 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Ammad
9 years ago

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

rk
13 years ago

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

Pradeep
10 years ago

Thanks a lot mkyong!

Subba
11 years ago

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.

afon
14 years ago

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

samba
10 years ago
Reply to  mkyong

I got above issue while using hibernate with JPA. It helped me in solving issue. Thank you.

Kevin
14 years ago

Worked for me, thanks!