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();
}
});
}
为什么?是冬眠的错误(3.3.2 GA和3.5.2Final)还是我误解了“ 1-base”的含义?
(基于1的序数异常在JPA中的行为有所不同)
然后我记得我用JPA1.0创建的最后一个项目,
我进行了演示,结果很好
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
最佳答案
您尚未显示HQL,但无论如何此消息都是令人误解的。如docs所述,在setParameter方法中,休眠参数从0开始。这就是为什么当您使用setParameter(i,arg [i])时它可以工作的原因。请记住,如果参数和参数个数不相同,您还将收到此消息,该消息可能想提醒一些有关元数据的信息,但会使开发人员感到困惑
关于java - 冬虫?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9274420/