Java 表中的JPA Unknow列在Spring中使用分页/页面别名

Java 表中的JPA Unknow列在Spring中使用分页/页面别名,java,spring,spring-boot,jpa,repository,Java,Spring,Spring Boot,Jpa,Repository,在spring中,通过自定义查询I请求实现分页: {{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

在spring中,通过自定义查询I请求实现分页:

{{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;
    }
}

为什么分页属性大小和页面仅在nativeQuery的某些情况下有效?

您还需要一个计数查询才能使分页正常工作,如下所示-

@Query(
  value = "select U.*, M.local as LocalM from user U inner join Morada M on M.idmorada = U.morada", 
  countQuery = "select count(*) from user U inner join Morada M on M.idmorada = U.morada", 
  nativeQuery = true)
Page<User> findUser(Pageable pageable);
#pageable占位符告诉Spring Data JPA如何解析查询并注入pageable参数。 就投影而言,您可以使用一个界面来映射您的结果集,如-

public interface IUser {
public getId();
... getter methods from User entity
public getLocalM();
}

我还不太熟悉Spring存储库,但是当查询选择所有用户列以及
m.local
时,您返回的是一页
User
实体-我假设框架在任何情况下都会遇到这个问题。但是假设用户是一个投影,我需要几乎所有的U(User)字段和M(Morada)的一列中的别名。在这种情况下,达成解决方案的最佳方法是什么?
@Query(
  value = "select U.*, M.local as LocalM from user U inner join Morada M on M.idmorada = U.morada", 
  countQuery = "select count(*) from user U inner join Morada M on M.idmorada = U.morada", 
  nativeQuery = true)
Page<User> findUser(Pageable pageable);
value = "select U.*, M.local as LocalM from user U inner join Morada M on M.idmorada = U.morada order by U.id \n-- #pageable\n"
public interface IUser {
public getId();
... getter methods from User entity
public getLocalM();
}