1

I want to use pagination with a native query. I use for this this syntaxe as in this example : Spring Data and Native Query with pagination

and it's my query:

@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);

I get this error :

Cannot mix JPA positional parameters and native Hibernate positional/ordinal parameters

Akino
  • 303
  • 1
  • 4
  • 16
  • Shouldn't there be a `}` after `?#{#pageable.offset + #pageable.pageSize`? – ialex Nov 16 '17 at 09:46
  • Yes @ialex and thank you for your comment but it's not the only problem. I've put the change I done to the query as solution for my question so this can help someone else. – Akino Nov 23 '17 at 09:57

1 Answers1

0

This is the solution I found to my problem:

@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);

I share it with you perhaps it can help someone else!

Akino
  • 303
  • 1
  • 4
  • 16