Spring Data JPA Custom Query limit functionality not working
Try to pass a method parameter with type Pageable
:
@Query("select q from SecurityQuestion q order by q.questionId asc")
public List<SecurityQuestion> findQuestion(Pageable page);
and when you call this function, init page
like this:
securityQuestionRepository.findQuestion(new PageRequest(0, 3));
Or just use native query:
@Query(nativeQuery = true,
value = "select * from user_security_question order by question_id asc limit 0, 3")
public List<SecurityQuestion> findQuestion();
Spring Boot CrudRepository or JpaRepository - how to pass limit as argument?
From spring doc (Limiting query results):
The results of query methods can be limited via the keywords first or top, which can be used interchangeably. An optional numeric value can be appended to top/first to specify the maximum result size to be returned. If the number is left out, a result size of 1 is assumed.
So, for fixed limit N
you can use List<Example> findTopNByValidIsTrue()
For variable value of limit you should use Pageable:
Page<Example> findByValidIsTrue(Pageable pageable);
List<Example> result = repository.findByValidIsTrue(new PageRequest(0, N)).getContent();
Related Topics
How to Close a Javafx Application on Window Close
Why Do I Get 404 for Rest With Spring-Boot
How to Skip @Postconstruct When Unit Testing
How to Identify File Type by Base64 Encoded String of a Image
Java Stream Collectors.Groupingby() Multiple Fields
Could Not Extract Resultset in Hibernate
Spring Junit: How to Mock Autowired Component in Autowired Component
Java: How to Print Array Without Square Brackets and Commas
How to Store a String Longer Than 4000 Characters in an Oracle Database Using Java/Jdbc
How to Convert a One-Dimensional Array to Two-Dimensional Array in Java
How to Update Only the Incoming Fields in a Model Using Spring Data Jpa
Java.Exe Finished With Non-Zero Exit Value 1
Mockito Mock Objects Returns Null
Get Current Week Start and End Date in Java - (Monday to Sunday)
Splitting Data Inside Quotes and Comma Using Regex
Java.Lang.Illegalargumentexception: No Converter Found for Return Value of Type