The problem here is in the two different techniques beeing applied.
- Firstly there is a pagination correctly applied on the DB Server. It will return the intended number of rows (e.g. 5).
- The second part is the Application part, where the Hibernate does select DISTINCT values from these 5 records.
So if there are in fact 2 rows doubled plus 1 other, the transformation will result in 3 objects.
The correct (and maybe the best) way, how to avoid that, is to not use fetching of the collections. If we need collection to be displayed, we should load it lazily (e.g. using batch-size to reduce number of selects)
If we need collection to be used as a filter, we should convert it into subquery
, and again do the pagination on the root entity, with the IN (Subquery) clause in place
Imagine this, Parent table:
ParentId, Code
1 , 'P1'
2 , 'P2'
3 , 'P3'
The child table:
ChildId , Code , ParentId
1 , 'C1' , 1
2 , 'C2' , 1
3 , 'C3' , 2
4 , 'C4' , 2
5 , 'C5' , 3
If we will ask for a Parent
and also join the Child
collection, we will
- recieve 5 rows on the DB server,
- which will be converted into only 3 Distinct Parent objects on the Application level