I am accessing a database table. It works if I provide the sortBy (a String) and orderBy (a String) to sort that table. I am able to get the sorted result. It also works if I retrieve that table and apply my pagination utility. I am able to disply my web pages; say, 10 records, at a time.
First of all, allow me to provide a short description of the pagination utility. This utility is "not" for front end. This utility makes as many trips to the database table as need to retrieve 10 records at a time till the maximum is reached.
I am having problem to do both the sort and the pagination. I am able to get the "first 10 sorted" records. But, no record is found when I try to display the "second 10 sorted" records. I think a critical piece is missing in my code and I need help. Here is my code:
Code:
public List getSortedUserList( int startIndex, int endIndex, String sortBy, String orderBy ) throws Exception
{
try
{
if( startIndex >=0 && endIndex <= startIndex )
throw new RemoteException( "getUserList endRowIndex should be greater than startRowIndex" );
Session session = HibernateUtil.getSession();
Criteria crit = session.createCriteria(User.class);
if ( orderBy.equals( "DESC" ) )
{
crit.addOrder( Order.desc( sortBy ) );
{
crit.addOrder( Order.asc( sortBy ) );
}
if( startIndex >=0 )
{
crit.setFirstResult( startIndex );
crit.setMaxResults( endIndex-startIndex+1 );
}
return crit.list();
} catch ( HibernateException e )
{
throw new RemoteException( "Hibernate Excetpion occured in getUserList", e );
}
}
[/u][/code]