In some cases Hibernate's criteria construction like "criteria.setProjection(Projections.rowCount())" returns wrong number of rows. More specifically, for example, if you have some JPA restrictions in you entity annotations (e.g.
@JoinColumn(name = "USER_ID", nullable = false) <---- this one!
public UserImpl user; ")
then such restrictions (for USER_ID is not null) will not be taken in account while building "row-count" query.
Vice-versa hibernate will use such restrictions in query for getting LIST of entities.
This behaivour fails, when in entity table appear (e.g. some old) rows with NULL in "USER_ID" (e.g.) columns.
This is known, but not solved problem for Hibernate, see:
There by we go into error in HibernateGridDataSource.getRowValue(int index) method: it throws IndexOutOfBoundsException due to row count number is bigger than actual number of selected rows.