I have the following Oracle SQL query:
SELECT user FROM global_users user WHERE user.status = 'ACTIVE'
AND user.description IS NOT NULL AND user.updatedGoodsDate BETWEEN '2024-03-10 20:09:53' AND '2024-03-10 20:09:53'
AND ROWNUM <= 13
I tried to edit the query into Postgres with Spring Data JPA:
@Query("SELECT user FROM global_users user WHERE user.status = :status
AND user.description IS NOT NULL AND user.updatedGoodsDate BETWEEN :startDate AND :endDate
AND LIMIT :count")
List<Users> findTopUsers(@Param("status") TransactionStatus status,
@Param("startDate") OffsetDateTime start, @Param("endDate") OffsetDateTime end, @Param("count") long count);
But for LIMIT :count
I get <expression> expected, got ':'
. Do you know how I can fix this?
2
Answers
I believe JPA does not support adding parameters for LIMIT.
You should be able to use Pageable to limit your result set, as shown in this answer: JPA limit functionality
In your case, this would look like:
@Query("SELECT user FROM global_users user WHERE user.status = :status
AND user.description IS NOT NULL AND user.updatedGoodsDate BETWEEN :startDate AND :endDate)
List findTopUsers(@Param("status") TransactionStatus status,
@Param("startDate") OffsetDateTime start, @Param("endDate") OffsetDateTime end, Pageable pageable);
Starting from SpringDataJPA 3.2 is possible to use Limit type.
As described here https://www.baeldung.com/jpa-limit-query-results#3-using-limit :
Official documentation of Spring here: https://docs.spring.io/spring-data/jpa/reference/repositories/query-methods-details.html#repositories.special-parameters