Pagination with Hibernate Criteria and DISTINCT_ROOT_ENTITY

后端 未结 3 438
猫巷女王i
猫巷女王i 2020-12-05 03:48

I\'ve have already implemented pagination using the following code:

public Paginacao consultarPaginado(int pagina, Integer cidadeId) {

               


        
3条回答
  •  Happy的楠姐
    2020-12-05 03:51

    I'm not sure if I understand your question correctly, but if your query retrieves entities with toMany associations join-fetched, then pagination won't work as expected.

    Indeed, suppose you have 4 A entities, each with 3 B entities, and suppose your query retrieves all the A entities with their Bs.

    In that case, the SQL query will return 12 rows. If you use setMaxResults(7), it will retrieve (for example) three rows for A1 and its Bs, three rows for A2 and its Bs, and just 1 row for A3 and its first B.

    And since you have used DISTINCT_ROOT_ENTITY, the criteria query will return only three entities: A1, A2, and A3 (which will have an incomplete set of Bs).

提交回复
热议问题