Issue
I have the following query for my JPA application against Postgres DB:
@Query("SELECT c FROM Cacl c WHERE c.name LIKE 'caclId%'")
public Page<Cacl> getCaclIdStartsWith(@Param("caclId") String caclId);
When I start the application I get the following exception:
Error creating bean with name 'caclRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Paging query needs to have a Pageable parameter! Offending method public abstract org.springframework.data.domain.Page .repositories.CaclReposit...(I'm masking here on purpose)... ory.getCaclIdStartsWith(java.lang.String)
The implication is that the result would not return multiple records. However it should return multiple records. In fact when I run SQL equivalent query:
SELECT * FROM TBL_CACL WHERE NAME LIKE 'SOME_CACL%';
, it works fine and returns multiple rows as I would expect. I assume I am doing something wrong with my syntax but not sure what. Grateful for any insights. Thanks
Solution
Your exception explicitly says: java.lang.IllegalArgumentException: Paging query needs to have a Pageable parameter!
So, you should correct the method signature in this way:
@Query("SELECT c FROM Cacl c WHERE c.name LIKE :caclId || '%'")
public Page<Cacl> getCaclIdStartsWith(@Param("caclId") String caclId, Pageable pageable);
and then to access the second page of Cacl
by a page size of 20, you could do something like the following:
Page<Cacl> cacls = repository.getCaclIdStartsWith("test", PageRequest.of(1, 20));
See also this section of the documentation.
Answered By - SternK
Answer Checked By - Clifford M. (JavaFixing Volunteer)