-->
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.  [ 3 posts ] 
Author Message
 Post subject: Faq does not appead to complet ans. count of query results?
PostPosted: Thu Jul 22, 2004 3:52 pm 
Beginner
Beginner

Joined: Mon Mar 22, 2004 9:37 am
Posts: 22
Location: Willow Grove, PA
I don't want to be annoying.

However, I have looked through the faq.

I have found
Code:
( (Integer) s.createFilter( collection, "select count(*)" ).iterate().next() ).intValue()


However, how does this help for named queries?

I am using the following code with "name" is supplied parameter of the named query we want to execute.

Code:
   public ResultInfo getPagedResults(final String name, final int pageSize, final int currentPage) throws ServiceException{
      
      return (ResultInfo) getHibernateTemplate().execute(new HibernateCallback() {
         public Object doInHibernate(Session session) throws HibernateException {
            Query q = session.getNamedQuery(name);
            
            
            int resultCount = ((Integer) session.createFilter( q.list(), "select count(*)" ).iterate().next()).intValue();
            q.setCacheable(true);
            q.setMaxResults(pageSize);
            q.setFirstResult(currentPage * pageSize);
            
            ResultInfo ri = new ResultInfo(q.list(), resultCount);
            return ri;      
         }
      });
         
   }



I am afraid that the q.list() below already is returning a full list from the database.
Code:
   int resultCount = ((Integer) session.createFilter( q.list(), "select count(*)" ).iterate().next()).intValue();


Is there a solution that will work for any named query(and mysql 4.0ish, ie. no subselects)

Thank you,

Matthew Payne[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 08, 2004 10:11 am 
Beginner
Beginner

Joined: Wed Sep 17, 2003 10:43 am
Posts: 48
Can someone please verify on whether the full list is *loaded* from the database in the last line of code posted by Matthew?

I was very suprised when i found out that


Code:
( (Integer) s.createFilter( collection, "select count(*)" ).iterate().next() ).intValue();


does not initialize the collection, so I was hoping this could also work.

Thanks!


Top
 Profile  
 
 Post subject: Counting query results
PostPosted: Wed Sep 08, 2004 10:46 am 
Beginner
Beginner

Joined: Wed Sep 17, 2003 10:43 am
Posts: 48
... which doesn't seem to work since filters are only applicable to persistent collections :-(

Supposing you have a Query object (with all parameters set) and a Session object, whats the more efficient way to count the results if you want to page? All I could think of was



Code:
            query.setCacheable(true);
            int totalResults = query.list().size();
            // now page
            query.setFirstResult((pageNumber-1) * pageSize);
            query.setMaxResults(pageSize + 1);
            List page = query.list();


isn't there a better way?

Thanks,

Manos


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.