Hi,
I am using some pagination to display my objects.
So I need to get my list ob objet and the total number of objects ...
In version 2, I used Criteria.count() to get the total number of results.
I just moved to version 3. So now I will have to use projections.
I just added it in my code with :
criteria.setProjection(Projections.rowCount());
The thing is, it modifies my criteria. SO, then, want I want to retrive my list of result I can't do it anymore !!!
Should I duplicate my criteria ? Then if yes, how, since I don't see any method for it...
My previous code :
Code:
public Page(Criteria criteria , String noPage, int pageSize)
throws HibernateException {
this.page = new Integer(noPage).intValue();
this.pageSize = pageSize;
int initialRow = page * pageSize;
int maxNbRows = pageSize + 1; // +1 pour savoir si il y a un suivant
criteria.setFirstResult( initialRow );
criteria.setMaxResults(maxNbRows);
// calcul le nombre de total de resultats
this.totalResults = new Long(criteria.count()).intValue();
results = criteria.list();
}
My actual code that does not work :
Code:
public Page(Criteria criteria , String noPage, int pageSize)
throws HibernateException {
this.page = new Integer(noPage).intValue();
this.pageSize = pageSize;
int initialRow = page * pageSize;
int maxNbRows = pageSize + 1; // +1 pour savoir si il y a un suivant
criteria.setFirstResult( initialRow );
criteria.setMaxResults(maxNbRows);
Criteria countCriteria = criteria.setProjection(Projections.rowCount());
Integer result = (Integer) countCriteria.uniqueResult();
this.totalResults = result.intValue();
results = criteria.list();
}
Note : I was thinking to just retrieve the row count after retrieving my list. That works, but only when I don't use setFirstResult and setMaxResults since it seems row counting does not like to have "limit ?,?" in the query. ANd I need that for my pagination !!!
thanks for help