4

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

2 Answers 2

7

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();
    }
Sign up to request clarification or add additional context in comments.

Comments

0

Limit is not supported in HQL. To set the size, you would use setMaxResults. To set start point, you use setFirstResult.

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

If you don't want to do it that way, you would have to use createSQLQuery to write a native sql query instead of hql.

1 Comment

Op specifically asked about a case using the @Query annotation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.