How to add restriction to Hibernate request annotation?

I have it in Java Hibernate

@Query("SELECT dirPar FROM DirectiveParagraph dirPar, Directive dir  "
        + "WHERE dirPar.directive = dir "
        + "AND dir.txtDirCode = :txtDirCode ");

List<DirectiveParagraph> getByName(@Param("txtDirCode") String name, @Param("page") int page ,@Param("size") int size);

I want to get with restriction and size, just like this one

SELECT * FROM tblDirectiveParagraph where intDirectiveID = 1 limit 10,10;

How to add limit to @Query annotation

+4
source share
2 answers

You can try adding the pageable parameter to your getByName method.

    @Query("SELECT dirPar FROM DirectiveParagraph dirPar, Directive dir  "
    + "WHERE dirPar.directive = dir "
    + "AND dir.txtDirCode = :txtDirCode ");
    Page<DirectiveParagraph> getByName(@Param("txtDirCode") String name, Pageable page);

And here is the method method call:

    public void someMethod(){
        PageRequest pageR = new PageRequest(10,10);
        Page<DirectiveParagraph> result = directiveParagraphRepository.getByName("txtDirCode",pageR);
        List<DirectiveParagraph> resultList = result.getContent();
    }
+7
source

The limit is not supported in HQL. To set the size, you must use setMaxResults. To set the starting point, you use setFirstResult.

Query q = session.createQuery("...");
q.setFirstResult(10);
q.setMaxResults(10);

If you do not want to do this, you will need to use createSQLQuery to write your own SQL query instead of hql.

0

Source: https://habr.com/ru/post/1545120/


All Articles