java H2 db:如何解决函数TOP找不到; SQL语句

dw1jzc5e  于 2023-01-11  发布在  Java
关注(0)|答案(1)|浏览(159)

嗨,我写了查询使用Spring规格,它看起来像这样。

select
        distinct TOP(?) project0_.idproject as idprojec1_40_,
        project0_.status as status2_40_ 
    from
        project project0_ 
    inner join
        project_info projectinf1_ 
            on project0_.idproject=projectinf1_.idproject 
    where
        lower(projectinf1_.proj_name) like ?

使用真实的数据库可以正常工作,但当我使用H2本地数据库时,我在控制台中得到了这个错误:

2017-10-16 18:58:56.808  WARN 14500 --- [io-8090-exec-10] o.h.engine.jdbc.spi.SqlExceptionHelper   : SQL Error: 90022, SQLState: 90022
2017-10-16 18:58:56.808 ERROR 14500 --- [io-8090-exec-10] o.h.engine.jdbc.spi.SqlExceptionHelper   : Function "TOP" not found; SQL statement:

应用程序.yml

spring:

  datasource:    
    driverClassName: org.h2.Driver
    url: jdbc:h2:file:~/project-data/dev;MODE=MSSQLServer
    username: sa
    password:
    initialize: True

  jpa:
    properties:
      hibernate:        
        ddl-auto: create 
        dialect: org.hibernate.dialect.SQLServer2008Dialect

        hbm2ddl:
          auto: create  
          import_files: enums.sql, import.sql

有什么建议如何解决它?

编辑:

用途

Page<Project> page = projectRepository.findAll(getSpecifications(params), 
getPaging(params));

static Pageable getPaging(SearchParams params) {
    Integer pageNumber = params.getPageNumber() != null ? params.getPageNumber() : 0;
    Integer pageSize = params.getPageSize() != null ? params.getPageSize() : 15;
    return new PageRequest(pageNumber, pageSize);
}

static Specifications<Project> getSpecifications(SearchParams params) {
    return Specifications.where(new SearchSpecifications(params));
}

public class SearchSpecifications implements Specification<Project> {
    // some methods
    @Override
    public Predicate toPredicate(Root<Project> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
    // conditions
    }
}
kknvjkwl

kknvjkwl1#

H2不支持像这样的select(同时使用distinct和top):select distinct top X attribute而其他引擎有,我想这就是原因。

相关问题