-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: J2EE Sort and Pagination Query Statements
PostPosted: Tue Jul 11, 2006 11:57 am 
Newbie

Joined: Tue Jul 11, 2006 11:11 am
Posts: 2
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]


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.