Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add limit to Hibernate query annotation?

I have this 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 retrieve with limit and size, same way like this

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

How do I add limit to above @Query annotation

like image 897
Sardor I. Avatar asked Mar 19 '23 07:03

Sardor I.


1 Answers

You can try adding a 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's a sample method call:

    public void someMethod(){
        PageRequest pageR = new PageRequest(10,10);
        Page<DirectiveParagraph> result = directiveParagraphRepository.getByName("txtDirCode",pageR);
        List<DirectiveParagraph> resultList = result.getContent();
    }
like image 82
Adrian Viesca Avatar answered Mar 22 '23 04:03

Adrian Viesca