我创建了一个自定义查询
@Repository
public interface SecurityQuestionRepository extends JpaRepository<SecurityQuestion, Integer>{
@Query("select q from SecurityQuestion q order by q.questionId asc limit 0, 3 ")
public List<SecurityQuestion> findQuestion();
}
当我致电该服务并调用公共List<SecurityQuestion> findQuestion()
方法时limit 0,3
无效
这是休眠日志,
Hibernate: select securityqu0_.question_id as question1_4_, securityqu0_.question_desc as question2_4_ from user_security_question securityqu0_ order by securityqu0_.question_id asc
如何使此限制功能在此工作?
答案 0 :(得分:7)
尝试传递类型为Pageable
的方法参数:
@Query("select q from SecurityQuestion q order by q.questionId asc")
public List<SecurityQuestion> findQuestion(Pageable page);
当你调用这个函数时,init page
就像这样:
securityQuestionRepository.findQuestion(new PageRequest(0, 3));
或者只使用原生查询:
@Query(nativeQuery = true,
value = "select * from user_security_question order by question_id asc limit 0, 3")
public List<SecurityQuestion> findQuestion();
答案 1 :(得分:2)
有一个更简单的变体:
public interface SecurityQuestionRepository extends JpaRepository<SecurityQuestion, Integer>{
List<SecurityQuestion> findTop3ByOrderByIdAsc();
}
更多信息是here。
答案 2 :(得分:0)
在nativeQuery = true, value = "your query"
注释中添加参数@Query
。