【发布时间】:2020-04-21 23:36:58
【问题描述】:
在春季使用我请求的自定义查询实现分页:
{{host}}:8080/list?page=0&size=2 and the result is OK
{{host}}:8080/list?page=0&size=3 and the result is OK
{{host}}:8080/list?page=0&size=4 and the result is OK
{{host}}:8080/list?page=0&size=1 and the result is NOT OK
{{host}}:8080/list?page=1&size=1 and the result is NOT OK
{{host}}:8080/list?page=1&size=2 and the result is NOT OK
{{host}}:8080/list?page=1&size=3 and the result is NOT OK
控制器:
@GetMapping(value = "/list")
public Page<User> list(Pageable pageable) {
try {
return userRepository.findUser(pageable);
} catch (Exception e) {
logger.error("Ex: {}", e);
return null;
}
}
存储库:
@Query(value = "select U.*, M.local as LocalM from user U inner join Morada M on M.idmorada = U.morada", nativeQuery= true)
public Page<User> findUser(Pageable pageable);
响应不正确时会发生什么:
2020-01-03 11:34:01.659 WARN 9652 --- [nio-8080-exec-3] o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Error: 1054, SQLState: 42S22
2020-01-03 11:34:01.659 ERROR 9652 --- [nio-8080-exec-3] o.h.engine.jdbc.spi.SqlExceptionHelper : Unknown column 'U' in 'field list'
为什么分页属性大小和页面仅在使用 nativeQuery 的某些场景下有效?
【问题讨论】:
-
我对 Spring 存储库(还)不太熟悉,但是当您的查询选择所有用户列以及
M.local时,您会返回一个包含User实体的页面 - 我会假设框架在任何情况下都会绊倒。 -
但是想象用户是一个投影,我需要几乎所有的 U(用户)字段和 M(Morada)一列的别名。在这种情况下达成解决方案的最佳方法是什么?
标签: java spring spring-boot jpa repository