在JPA本机查询中可分页

时间:2017-11-16 09:12:11

标签: java spring hibernate pagination spring-data-jpa

我想使用本机查询分页。我在这个例子中使用了这个语法:Spring Data and Native Query with pagination

这是我的疑问:

@Query(value="SELECT rownum() as RN, users.num, users .l_service,service.type_service, users.date, " +
        "chambre.price," +
        "price* ( case when(datediff(day,date_d,date_f)=0) then 1 " +
        "else datediff(day,date_d,date_f) end ) as Montant," +
        "case when  (service.type_service='R') and  datediff(day,date_d,date_f) >=21 " +
        "then (21300+(datediff(day,date_d,date_f)-21)*200)" +
        "else price*(case when(datediff(day,date_d,date_f)=0) then 1 else datediff(day,date_d,date_f)end) end AS Montant_final " +
        " users.year, users.Etat, " +
        " from chambre JOIN users ON chambre.code = users.type " +
        "JOIN service on service.code = users.l_service " +
        " WHERE users.Etat='V' and RN between ?#{ #pageable.offset -1} and ?#{#pageable.offset + #pageable.pageSize order by users.num",
        countQuery ="select count(*) from users ",nativeQuery = true)
Page<Object> getStatistiques(Pageable pageable);

我收到此错误:

  

无法混合JPA位置参数和原生Hibernate位置/序数参数

1 个答案:

答案 0 :(得分:0)

这是我发现问题的解决方案:

@Query(value="SELECT * from (SELECT ROW_NUMBER() OVER (ORDER BY users.num) as RN, users.num, users .l_service,service.type_service, users.date, " +
    "chambre.price," +
    "price* ( case when(datediff(day,date_d,date_f)=0) then 1 " +
    "else datediff(day,date_d,date_f) end ) as Montant," +
    "case when  (service.type_service='R') and  datediff(day,date_d,date_f) >=21 " +
    "then (21300+(datediff(day,date_d,date_f)-21)*200)" +
    "else price*(case when(datediff(day,date_d,date_f)=0) then 1 else datediff(day,date_d,date_f)end) end AS Montant_final " +
    " users.year, users.Etat, " +
    " from chambre JOIN users ON chambre.code = users.type " +
    "JOIN service on service.code = users.l_service " +
    " WHERE users.Etat='V') AS STA where RN between ?#{ #pageable.offset -1} and ?#{#pageable.offset + #pageable.pageSize} order by STA.num",
    countQuery ="select count(*) from users ",nativeQuery = true) 
    Page<Object> getStatistiques(Pageable pageable);

我和你分享也许它可以帮助别人!