I'm using hibernate 3.6.3 Final version (I know it's pretty old, but that is like that for now on this project I'm currently working on).
I have a problem with doing joins and pagination in order that I am getting one record duplicated in results which is caused by hibernate.
This is the code I have:
public Page<T> findByCriteriaPaginated(PageParams params, Criteria countCriteria, Criteria listCriteria, String[] joins) {
Page<T> page = new Page<T>(params);
// count criteria
countCriteria.setProjection(Projections.rowCount());
page.setTotalCount(((Long) countCriteria.uniqueResult()).intValue());
// fetch criteria
listCriteria.setFirstResult(params.getFirstResultIdx());
listCriteria.setMaxResults(params.getPageSize());
if (params.getOrdering() != null && params.getOrdering().getSize() > 0) {
for (Iterator<String> it = params.getOrdering().getKeyIterator(); it.hasNext();) {
String key = it.next();
if (params.getOrdering().isAscending(key)) {
listCriteria.addOrder(Order.asc(key));
} else {
listCriteria.addOrder(Order.desc(key));
}
}
}
if (joins != null && joins.length > 0) {
for (String s : joins) {
listCriteria.setFetchMode(s, FetchMode.JOIN);
}
}
page.setResults(listCriteria.list());
return page;
}
When I hit the query that is generated and run it on DB server, then I don't have this duplicate record. But debuging my code, listCriteria.list()
returns data set with duplicate. Also when I comment out these two lines listCriteria.setFirstResult(params.getFirstResultIdx());
listCriteria.setMaxResults(params.getPageSize());
, then listCriteria.list()
has no duplicate, it is fine.
So, this indicates to me that there is some problem with pagination and the rest of criteria I'm having (with joins and ordering).
Does anybody have idea how to fix this? Is this hibernate bug? Would increasing hibernate version to the latest (5.2.9.Final) help? Are there any potential problems with such large version upgrade?
Thank you for any kind of help.
mismas