16:59:08,082 INFO org.springframework.test.context.transaction.TransactionalTestExecutionListener:274 - Began transaction (1): transaction manager [org.springframework.orm.hibernate3.HibernateTransactionManager@f90137]; rollback [true]java.lang.IndexOutOfBoundsException: Remember that ordinal parameters are 1-based!
at org.hibernate.engine.query.ParameterMetadata.getOrdinalParameterDescriptor(ParameterMetadata.java:79)
at org.hibernate.engine.query.ParameterMetadata.getOrdinalParameterExpectedType(ParameterMetadata.java:85)
at org.hibernate.impl.AbstractQueryImpl.determineType(AbstractQueryImpl.java:421)
at org.hibernate.impl.AbstractQueryImpl.setParameter(AbstractQueryImpl.java:393)
at com.luck.dao.BaseDao$1.doInHibernate(BaseDao.java:106)
at com.luck.dao.BaseDao$1.doInHibernate(BaseDao.java:1)
我的代码是:
public List<T> list(final String hql,final int start, final int limit,final Object ...args) {
return hibernateTemplate.executeFind(new HibernateCallback<List<T>>() {
public List<T> doInHibernate(Session session) throws HibernateException, SQLException {
Query q = session.createQuery(hql).setFirstResult(start).setMaxResults(limit);
if(args!=null&&args.length>0){
for (int i = 0; i < args.length; i++) {
q.setParameter(i+1, args[i]);
}
}
return (List<T>) q.list();
}
});
}
问题是: 当我改变'q.setParameter(i + 1,args [i]);到q.setParameter(i,args [i]),然后它可以工作。
public List<T> listJPA(final String hql,final int start, final int limit,final Object ...args) {
return hibernateTemplate.executeFind(new HibernateCallback<List<T>>() {
public List<T> doInHibernate(Session session) throws HibernateException, SQLException {
javax.persistence.Query q = entityManagerFactory.createEntityManager().createQuery(hql).setFirstResult(start).setMaxResults(limit);
if(args!=null&&args.length>0){
for (int i = 0; i < args.length; i++) {
q.setParameter(i+1, args[i]);
}
}
return (List<T>) q.getResultList();
}
});
}
为什么呢?这是hibernate中的一个错误(3.3.2 GA和3.5.2Final)还是我误解了'1-base'的含义?
(基于1的序数异常在JPA中表现不同) 然后我记得我用JPA1.0创建的最后一个项目,
我制作了anthoer演示,因此,它运作良好
java.lang.IndexOutOfBoundsException: Remember that ordinal parameters are 1-based!
at org.hibernate.engine.query.ParameterMetadata.getOrdinalParameterDescriptor(ParameterMetadata.java:79)
at org.hibernate.engine.query.ParameterMetadata.getOrdinalParameterExpectedType(ParameterMetadata.java:85)
at org.hibernate.impl.AbstractQueryImpl.determineType(AbstractQueryImpl.java:421)
at org.hibernate.impl.AbstractQueryImpl.setParameter(AbstractQueryImpl.java:393)
at com.luck.dao.BaseDao$2.doInHibernate(BaseDao.java:124)
at com.luck.dao.BaseDao$2.doInHibernate(BaseDao.java:1)
at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:406)
at org.springframework.orm.hibernate3.HibernateTemplate.executeFind(HibernateTemplate.java:343)
at com.luck.dao.BaseDao.list(BaseDao.java:119)
at com.luck.dao.HistoryDaoTest.testHistory(HistoryDaoTest.java:24)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:46)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:180)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:41)
at org.junit.runners.ParentRunner$1.evaluate(ParentRunner.java:173)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
at org.junit.runners.ParentRunner.run(ParentRunner.java:220)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:46)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
17:21:30,714 INFO org.springframework.test.context.transaction.TransactionalTestExecutionListener:294 - Rolled back transaction after test execution for test context [[TestContext@43ad4a testClass = HistoryDaoTest, testInstance = com.luck.dao.HistoryDaoTest@125d568, testMethod = testHistory@HistoryDaoTest, testException = [null], mergedContextConfiguration = [MergedContextConfiguration@ddcd98 testClass = HistoryDaoTest, locations = '{classpath*:/application-context.xml}', classes = '{}', activeProfiles = '{}', contextLoader = 'org.springframework.test.context.support.DelegatingSmartContextLoader']]]
Hibernate: select history0_.bet_id as bet1_3_, history0_.bet as bet3_, history0_.num as num3_, history0_.time as time3_, history0_.type as type3_, history0_.win as win3_ from History history0_ where history0_.bet_id=? limit
17:21:30,780 INFO org.springframework.context.support.GenericApplicationContext:1020 - Closing org.springframework.context.support.GenericApplicationContext@102799c: startup date [Tue Feb 14 17:21:28 CST 2012]; root of context hierarchy
答案 0 :(得分:2)
您尚未展示HQL,但无论如何此消息都具有误导性。 Hibernate参数从setParameter方法中的0开始,如docs中所述。这就是为什么当你使用setParameter(i,arg [i])时它的工作原理。请记住,如果args和no参数不相同,您也会收到此消息,这可能想要提醒一些有关元数据但会让开发人员感到困惑
答案 1 :(得分:1)
很难在这里找到问题:)
序数1表示参数从1开始计数(不是0)。我通常倾向于使用setParameter(String,Object)方法不依赖于顺序而是依赖于查询参数的名称。
所以答案是否定的,那不是错误。它可能在某个地方的JPA规范中提到过。