【问题标题】:I am getting java.lang.IndexOutOfBoundsException: Remember that ordinal parameters are 1-based while doing bulk update我收到 java.lang.IndexOutOfBoundsException: 请记住,在进行批量更新时序数参数是基于 1 的
【发布时间】:2016-03-09 10:26:00
【问题描述】:

我试图在下面的查询中将列表作为参数传递,然后调用 Hibernate 批量更新,在调用我遇到异常时,请告诉我如何将列表设置为条件中的参数值。 查询如下---- 插入存档(cust_id,orderNum,国家,extractionType,errorCode,extractionDate)" + " select cust_id、orderNum、country、extractionType、errorCode、extractionDate from OrderTest where cust_id = :ids

我的java代码是

  public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
    Session session = sessionFactory.getCurrentSession();
    List<OrderTest> customerIdList = getPurgeCriteria(session);
    if (null != customerIdList) {
      StringBuffer cust_ids = new StringBuffer();

      String purgeStatement = "insert into OrderArchive(cust_id, orderNum, country, extractionType, errorCode, extractionDate)"
          + " select * from OrderTest where cust_id in (:ids)";
      // Criteria purgeCriteria =
      // Criteria.createCriteria(OrderTest.class,purgeStatement);
      Query purgeQuery = session.createQuery(purgeStatement);
      // purgeQuery.setParameterList("ids", customerIdList);
      /*
       * for(String cust_id : customerIdList) {
       * cust_ids.append(cust_id).append(","); purgeCriteria.setParameter("ids",
       * cust_ids.toString()); System.out.println(cust_id); }
       */
      // purgeCriteria.setParameter("ids", customerIdList);
      // purgeCriteria.setParameter("ids", customerIdList);
      String deleteStatement = "delete from OrderTest where cust_id in (:delIds)";
      // Criteria deleteCriteria =
      // Criteria.createCriteria(OrderTest.class,deleteStatement);
      // deleteCriteria.setParameter("delIds", customerIdList);
      Query deleteQuery = session.createQuery(deleteStatement);
      deleteQuery.setParameterList("delIds", customerIdList);
      try {
        int res = purgeQuery.executeUpdate();
        // int res = purgePersistenceManager.update(purgeCriteria);
        logger.info("No of records inserted into archive table :" + res);
        if (res > 0) {
          int rows = deleteQuery.executeUpdate();
          // int rows = purgePersistenceManager.update(deleteCriteria);
          logger.info("No of records deleted from order test table :" + rows);
        }
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
    return null;
  }

java.lang.IndexOutOfBoundsException:记住序数参数 是从 1 开始的! 在 org.hibernate.engine.query.ParameterMetadata.getOrdinalParameterDescriptor(ParameterMetadata.java:55) 在 org.hibernate.engine.query.ParameterMetadata.getOrdinalParameterExpectedType(ParameterMetadata.java:61) 在 org.hibernate.impl.AbstractQueryImpl.determineType(AbstractQueryImpl.java:382) 在 org.hibernate.impl.AbstractQueryImpl.setParameter(AbstractQueryImpl.java:362) 在 org.springframework.orm.hibernate3.HibernateTemplate$39.doInHibernate(HibernateTemplate.java:1147) 在 org.springframework.orm.hibernate3.HibernateTemplate$39.doInHibernate(HibernateTemplate.java:1141) 在 org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:406) 在 org.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374) 在 org.springframework.orm.hibernate3.HibernateTemplate.bulkUpdate(HibernateTemplate.java:1141) 在 in.gov.uidai.platform.impl.persistence.handler.HibernateHandler.update(HibernateHandler.java:312) 在 in.gov.uidai.platform.impl.persistence.provider.AbstractPersistenceProvider.update(AbstractPersistenceProvider.java:110) 在 in.gov.uidai.platform.impl.persistence.PersistenceDelegate.update(PersistenceDelegate.java:152) 在 in.gov.uidai.platform.impl.persistence.PersistenceDelegate$$FastClassByCGLIB$$9f7b2e03.invoke() 在 org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204) 在 org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:698) 在 org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150) 在 org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:106) 在 org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) 在 org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:631) 在 in.gov.uidai.platform.impl.persistence.PersistenceDelegate$$EnhancerByCGLIB$$e4e1748.update() 在 in.gov.uidai.platform.impl.persistence.PersistenceManagerProvider.update(PersistenceManagerProvider.java:320) 在 in.gov.uidai.platform.impl.persistence.PersistenceManagerProvider.update(PersistenceManagerProvider.java:277) 在 batch.impl.tasklets.PurgeProcessor.execute(PurgeProcessor.java:88) 在 org.springframework.batch.core.step.tasklet.TaskletStep$ChunkTransactionCallback.doInTransaction(TaskletStep.java:406) 在 org.springframework.batch.core.step.tasklet.TaskletStep$ChunkTransactionCallback.doInTransaction(TaskletStep.java:330) 在 org.springframework.transaction.support.TransactionTemplate.execute(TransactionTemplate.java:128) 在 org.springframework.batch.core.step.tasklet.TaskletStep$2.doInChunkContext(TaskletStep.java:271) 在 org.springframework.batch.core.scope.context.StepContextRepeatCallback.doInIteration(StepContextRepeatCallback.java:77) 在 org.springframework.batch.repeat.support.RepeatTemplate.getNextResult(RepeatTemplate.java:368) 在 org.springframework.batch.repeat.support.RepeatTemplate.executeInternal(RepeatTemplate.java:215) 在 org.springframework.batch.repeat.support.RepeatTemplate.iterate(RepeatTemplate.java:144) 在 org.springframework.batch.core.step.tasklet.TaskletStep.doExecute(TaskletStep.java:257) 在 org.springframework.batch.core.step.AbstractStep.execute(AbstractStep.java:198) 在 org.springframework.batch.core.job.SimpleStepHandler.handleStep(SimpleStepHandler.java:148) 在 org.springframework.batch.core.job.flow.JobFlowExecutor.executeStep(JobFlowExecutor.java:64) 在 org.springframework.batch.core.job.flow.support.state.StepState.handle(StepState.java:67) 在 org.springframework.batch.core.job.flow.support.SimpleFlow.resume(SimpleFlow.java:162) 在 org.springframework.batch.core.job.flow.support.SimpleFlow.start(SimpleFlow.java:141) 在 org.springframework.batch.core.job.flow.FlowJob.doExecute(FlowJob.java:134) 在 org.springframework.batch.core.job.AbstractJob.execute(AbstractJob.java:304) 在 org.springframework.batch.core.launch.support.SimpleJobLauncher$1.run(SimpleJobLauncher.java:135) 在 org.springframework.core.task.SyncTaskExecutor.execute(SyncTaskExecutor.java:49) 在 org.springframework.batch.core.launch.support.SimpleJobLauncher.run(SimpleJobLauncher.java:128) 在 batch.impl.jobs.BatchJob.execute(BatchJob.java:48) 在 org.quartz.core.JobRunShell.run(JobRunShell.java:199) 在 org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:549

【问题讨论】:

  • 尝试打印索引和值以检查代码在什么时候失败。
  • Dishi 如您所见,我正在获取一个 id 列表,然后我将其设置为使用命名参数的标准参数......在我的内部项目中实现的标准类调用 HibernateTemplate 的 Update 方法。

标签: java hibernate hibernate-criteria


【解决方案1】:

尝试单独执行此查询,因为认为此查询提供多行:

 " select * from OrderTest where cust_id in (:ids)";

为此它给出了错误

【讨论】:

  • @JavaStack 它给了你什么?
  • 我遇到了同样的异常
  • @JavaStack 我的意思是上面有多少行给你的查询:“ select * from OrderTest where cust_id in (:ids)”
  • 返回多行
  • @JavaStack 我认为这是你应该遇到的问题一行
【解决方案2】:

首先执行 purgeStatement 然后应用 select Query。 如果要更新数据,请使用 save 或 update 并在其中使用 where 子句

String purgeStatement = "插入 OrderArchive(cust_id, orderNum, country, extractType, errorCode, extractDate)";

String selectStatement ="select * from OrderArchive where cust_id in (:ids)" ;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-03-03
    • 1970-01-01
    • 1970-01-01
    • 2019-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多