-->
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.  [ 6 posts ] 
Author Message
 Post subject: Hibernate HQL count without list, is it possible?
PostPosted: Wed Apr 07, 2010 12:15 pm 
Newbie

Joined: Wed May 27, 2009 11:59 am
Posts: 10
Hi,
It sounds like a very basic and simple need,

is it possible to count rows from hibernate query without calling the list?

If not, is there a efficiency cost for calling this list for this purpose? (Otherwise I would call it only after adding pagination).


Top
 Profile  
 
 Post subject: Re: Hibernate HQL count without list, is it possible?
PostPosted: Thu Apr 08, 2010 5:03 am 
Regular
Regular

Joined: Wed Mar 10, 2010 4:48 am
Posts: 106
You could use a query:
Code:
SELECT count(*) FROM ...
Or use Criteria:
Code:
int i = (Integer) session.createCriteria(Object.class)
               .setProjection(Projections.rowCount())
               .add(Restrictions.eq("columnName", value)).list().get(0);

This will make a list but it only contains the count.


Top
 Profile  
 
 Post subject: Re: Hibernate HQL count without list, is it possible?
PostPosted: Thu Apr 08, 2010 7:50 pm 
Newbie

Joined: Wed May 27, 2009 11:59 am
Posts: 10
Of course I can do that,
but I asked about HQL and the ability to count HQL query result rows.

Solutions such as building additional query with select(*) are not pretty, solutions that suggest manipulating the hql query string manually are also very risky.

The solution I would have done in proper SQL would be:

Code:
select count(*) from (SQL Query)


But I don't think it is an option here since I'm not sure I can get the query parameters, or clone a query, and also this syntax doesn't seem to be supported by HQL.

Thanks,
Roee.


Top
 Profile  
 
 Post subject: Re: Hibernate HQL count without list, is it possible?
PostPosted: Fri Apr 09, 2010 3:03 am 
Regular
Regular

Joined: Wed Mar 10, 2010 4:48 am
Posts: 106
I believe this is HQL (could be mistaken):
Code:
   public static int getCount(Class<?> c)
   {
      int i = 0;
      Session session = SessionHandler.getSession();
      try
      {
         List<Long> list = session.createQuery("SELECT count(*) FROM " + c.getName()).list();
         i = list.get(0).intValue();
      }
      catch (Exception e)
      {
         Logger.get().error(e.getMessage());
         i = 0;
      }
      SessionHandler.closeSession(session);
      return i;
   }
If it was native SQL I would need to use session.createSQLQuery()


Top
 Profile  
 
 Post subject: Re: Hibernate HQL count without list, is it possible?
PostPosted: Mon Apr 26, 2010 8:22 am 
Newbie

Joined: Wed May 27, 2009 11:59 am
Posts: 10
Sorry,
but this is not what I asked.
I want to get as parameter HQL query for example (Select * from a where a.b=c.d.....)
and return the count of rows it would return.


Top
 Profile  
 
 Post subject: Re: Hibernate HQL count without list, is it possible?
PostPosted: Mon Apr 26, 2010 8:35 am 
Regular
Regular

Joined: Wed Mar 10, 2010 4:48 am
Posts: 106
I'm sorry but I really don't understand what it is you want.

Perhaps you could rephrase your question, or maby somone else understands it.


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