-->
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.  [ 2 posts ] 
Author Message
 Post subject: How to get paging's total number of records?
PostPosted: Sat Mar 31, 2007 12:53 am 
Beginner
Beginner

Joined: Tue Feb 13, 2007 9:29 pm
Posts: 21
1.2 beta sql server 2000

Hi,

I started to experiment with the build-in paging function for my forum project and I was curios on how NHibernate does it.

I did

Code:
q.SetMaxResults(50);

//first page
q.SetFirstResult(0);

//second page
//q.SetFirstResult(50);



I noticed if I set paging to 50, for 1st page, it did select Top 50, but for 2nd page, it did select Top 100??

that doesn't seem right... I guess it's a not a big deal since I don't think my forum will grow beyound couple thousands of threads... but still doesn't seem to be the best way. (but i did do a test of 10,000 threads, all got done in 4 seconds. Used the .NET SqlDataReader as well, 4 seconds as well, so no performance hit as far as my requirements), but any advice on that? How do you do paging in your app?

Anyway, back to .setFirstResult(). To properly figure out paging, I have to know how many pages there is gonne be. How do you do that? I suppose I can do

Code:

if(  (total records % pageSize)==0)
{
  number_of_pages = total records / pageSize;
}
else{
  number_of_pages = total records / pageSize + 1;
}



but then, how do I figure out the total records in one query without doing a seperate db call?


Here is my existing query

Code:

IQuery q = session.CreateQuery(
              "select discussions.DiscussionID, discussions.DiscussionTitle, discussions.CreatedBy, count(comments) " +
              "from JumptreeForum_Discussions as discussions " +
              "left outer join discussions.Comments as comments " +
              "where :categoryid in " +
              "( " +
              "   select categories.CategoryID from discussions.Categories categories " +
              " ) " +
              "group by discussions.DiscussionID, discussions.DiscussionTitle, discussions.CreatedBy"
            );

       

            q.SetInt32("categoryid", Convert.ToInt32(categoryid));
            q.SetMaxResults(50);
            q.SetFirstResult(50*pageNumber); // how do i get pagenumber?
            result = q.List();




Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Apr 01, 2007 1:18 am 
Beginner
Beginner

Joined: Tue Feb 13, 2007 9:29 pm
Posts: 21
Okay, I was lucky to finally found the blog by none other than Ayende again.

http://www.ayende.com/Blog/archive/7060.aspx

I solved the problem by using .CreateMultiQuery(), both select in one batch.

Code:

IList multi_results = session.CreateMultiQuery()
                     .Add(
                        session.CreateQuery(
                              " select discussions.DiscussionID, discussions.DiscussionTitle, discussions.CreatedBy, count(comments) " +
                              "from JumptreeForum_Discussions as discussions " +
                              "left outer join discussions.Comments as comments " +
                              "where :categoryid in " +
                              "( " +
                              "   select categories.CategoryID from discussions.Categories categories " +
                              " ) " +
                              "group by discussions.DiscussionID, discussions.DiscussionTitle, discussions.CreatedBy"
                            ).SetInt32("categoryid", Convert.ToInt32(categoryid))
                            .SetFirstResult(0).SetMaxResults(50)
                        )
                     .Add(session.CreateQuery(
                             " select count(*) " +
                              "from JumptreeForum_Discussions as discussions " +
                              "left outer join discussions.Comments as comments " +
                              "where :categoryid2 in " +
                              "( " +
                              "   select categories.CategoryID from discussions.Categories categories " +
                              " ) "
                        ).SetInt32("categoryid2", Convert.ToInt32(categoryid))
                      )
                      .SetCacheable(true)
                      .List();
            IList result = (IList)multi_results[0];
            long total_count = (long)((IList)multi_results[1])[0];




problemo solved! very cool Thanks anyway.


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

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.