jpa hib在DB2中呈现的SQL错误?

kyks70gy  于 2022-11-23  发布在  DB2
关注(0)|答案(1)|浏览(121)

我认为Hibernate在DB2中呈现了错误的SQL?我当然犯了一个错误
我jpa代码是:

public List<Gc1glcostctrmap> findByCriteria(
        Map<String, SortDirectionEnum> sortFields,
        Integer start,
        Integer length) throws Exception {

  CriteriaBuilder builder = entityManager.getCriteriaBuilder();
  CriteriaQuery<Gc1glcostctrmap> criteriaQuery = builder.createQuery(Gc1glcostctrmap.class);
 Root<Gc1glcostctrmap> root = criteriaQuery.from(Gc1glcostctrmap.class);

  int noDeleted = 0;
  criteriaQuery.select( root )
        .where( builder.equal(root.get(Gc1glcostctrmap_.g1011logdel),  noDeleted) );

  if ( Validator.isNotNull(sortFields) ) {
    criteriaQuery.orderBy(_orderBy(sortFields, builder, root));
  }

    // create query
  Query query = entityManager.createQuery(criteriaQuery);
  query.setFirstResult(start);
  query.setMaxResults(length);

    // execute query
  List<Gc1glcostctrmap> gc1glcostctrmaps = query.getResultList();
  return gc1glcostctrmaps;
}

并且sqlshow为True,对于呈现的第一页:

select fields from BNGL.GC1GLCOSTCTRMAP gc1glcostc0_ where gc1glcostc0_.g1011logdel=0 order by gc1glcostc0_.g1136id asc fetch first 10 rows only

但是对于所呈现第二页:

select fields from ( select inner2_.*, rownumber() over( order by order of inner2_) as rownumber_ from 
( select fields from BNGL.GC1GLCOSTCTRMAP gc1glcostc0_  where gc1glcostc0_.g1011logdel=0 order by gc1glcostc0_.g1136id asc fetch first 20 rows only ) as inner2_ ) as inner1_  where rownumber_ > 10 
order by rownumber_

最终错误是:

2018-07-29 09:13:22.218  WARN 6684 --- [nio-8082-exec-9] o.h.engine.jdbc.spi.SqlExceptionHelper   : SQL Error: -199, SQLState: 42601
2018-07-29 09:13:22.219 ERROR 6684 --- [nio-8082-exec-9] o.h.engine.jdbc.spi.SqlExceptionHelper   : DB2 SQL Error: SQLCODE=-199, SQLSTATE=42601, SQLERRMC=OF;( . AT MICROSECONDS MICROSECOND SECONDS SECOND MINUTES MINUTE, DRIVER=3.68.61
2018-07-29 09:13:22.219  WARN 6684 --- [nio-8082-exec-9] o.h.engine.jdbc.spi.SqlExceptionHelper   : SQL Error: -516, SQLState: 26501
2018-07-29 09:13:22.219 ERROR 6684 --- [nio-8082-exec-9] o.h.engine.jdbc.spi.SqlExceptionHelper   : DB2 SQL Error: SQLCODE=-516, SQLSTATE=26501, SQLERRMC=null, DRIVER=3.68.61
2018-07-29 09:13:22.219  WARN 6684 --- [nio-8082-exec-9] o.h.engine.jdbc.spi.SqlExceptionHelper   : SQL Error: -514, SQLState: 26501
2018-07-29 09:13:22.219 ERROR 6684 --- [nio-8082-exec-9] o.h.engine.jdbc.spi.SqlExceptionHelper   : DB2 SQL Error: SQLCODE=-514, SQLSTATE=26501, SQLERRMC=SQL_CURLH200C1, DRIVER=3.68.61
2018-07-29 09:13:22.228 ERROR 6684 --- [nio-8082-exec-9] ir.bmi.service.impl.MapTstServiceImpl    : Error occurred while calling findByCriteria().
vom3gejh

vom3gejh1#

您可以使用自定义Dialect,因为Hibernate DB2Dialect中生成的查询不正确。请参考以下答案:db2-with-pagination

相关问题