将分页与查询注释Spring Data JPA一起使用时出错

时间:2018-11-01 19:13:33

标签: hibernate spring-boot pagination spring-data-jpa jpql

@Repository
public interface PriceListDurationRepository extends CrudRepository<PriceListDuration, String> {

    String mQuery = "SELECT qplt, qplab"    
                     + " ,(SELECT iuomt1.unitOfMeasure FROM UnitsOfMeasureTransTbl iuomt1) "
                 + " FROM PriceListDuration qplab, PriceListDetail qplt " 
                 + " WHERE qplab.priceListId           =qplt.priceListId ";

    @Query(value = mQuery + " AND esib.itemNumber = :itemnumber")
    Page<PriceListDuration> getByItemNumber(String itemnumber, Pageable pageable);
}

我在getByItemNumber方法中使用分页。但是由于列级的子查询,它返回错误。

如果我删除了

Page<PriceListDuration> getByItemNumber(String itemnumber, Pageable pageable)

List<PriceListDuration> getByItemNumber(String itemnumber);一起工作。分页似乎不支持子查询。是否可以使用分页对此方法进行分页?任何帮助将不胜感激

1 个答案:

答案 0 :(得分:0)

您可能需要countQuery,请尝试:

@Query(value =  mQuery + " AND esib.itemNumber = :itemnumber",
    countQuery = "SELECT count(*) FROM (" + mQuery + " AND esib.itemNumber = :itemnumber" + ")",
    nativeQuery = true)

此外,您可能还需要添加Order by

检查样品:

 @Query(
  value = "SELECT * FROM Users ORDER BY id", 
  countQuery = "SELECT count(*) FROM Users", 
  nativeQuery = true)
Page<User> findAllUsersWithPagination(Pageable pageable);

它是从这里来的:https://www.baeldung.com/spring-data-jpa-query