日期:2014-05-20  浏览次数:20841 次

java.lang.IndexOutOfBoundsException: Remember that ordinal parameters are 1-base
请问哪位高手知道这是什么原因吗??
抱这个错误:
exception in thread "main" java.lang.IndexOutOfBoundsException: Remember that ordinal parameters are 1-based!
at org.hibernate.engine.query.ParameterMetadata.getOrdinalParameterDescriptor(ParameterMetadata.java:55)
at org.hibernate.engine.query.ParameterMetadata.getOrdinalParameterExpectedType(ParameterMetadata.java:61)
at org.hibernate.impl.AbstractQueryImpl.determineType(AbstractQueryImpl.java:397)
at org.hibernate.impl.AbstractQueryImpl.setParameter(AbstractQueryImpl.java:369)
at org.liu.hibernate.dao.ProductTypeDao.getResult(ProductTypeDao.java:30)
at org.liu.hibernate.test.ProductTest.main(ProductTest.java:11)

相关代码如下:
主函数:
public <ProductType> QueryResult<ProductType> getResult(
Class<ProductType> cls, int indexBegin, int maxResult,
String queryString, Object[] obj) {

Session session = HibernateUtils.getSession();
Transaction trans = session.beginTransaction();
QueryResult<ProductType> query = new QueryResult<ProductType>();
String objectName = cls.getSimpleName();
StringBuffer sql = new StringBuffer("from " + objectName + " o");
if (queryString != null && !"".equals(queryString)) {
sql.append(" where " + queryString);
}
Query qr = session.createQuery(sql.toString());
if (obj != null && obj.length > 0) {
for (int i = 0; i < obj.length; i++){
qr.setParameter(i+1, obj[i]);
}
}
qr.setFirstResult(indexBegin);
qr.setMaxResults(maxResult);
query.setResultRecord(new ArrayList<ProductType>(qr.list()));
Query qrs = session.createQuery(sql.toString());
query.setTotalResult(qrs.list().size());
trans.commit();
session.close();
return query;
}

运行代码:
public class ProductTest {
public static void main(String[] ars) {
ProductTypeDao pro = new ProductTypeDao();
System.out.println(pro.getResult(ProductType.class, 1, 5, "o.parentId=?1",
new Object[]{1}).getTotalResult());
}
}



------解决方案--------------------
qr.setParameter(i+1, obj[i]); 
从0开始
qr.setParameter(i, obj[i]);
------解决方案--------------------
Java code
public class IndexOutOfBoundsException
extends RuntimeException
指示某排序索引(例如对数组、字符串或向量的排序)超出范围时抛出。