-->
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: Count & Criteria
PostPosted: Wed May 23, 2007 9:56 am 
Newbie

Joined: Wed May 23, 2007 9:14 am
Posts: 2
Hello,

is it possible to query for the amount of rows (objects) in a database?

Ok, I know it is possible by HQL query (select coutn(obj) from ...), but what about Cirteria queries?

I want to know how many objects for a certain criteria exist (e.g. 100 000), but I do not want NHibernate to create all the objects.

For example I have the following code:

Code:

....

Example example = Example.Create( myPerson )
.IgnoreCase()
.ExcludeNulls()
.EnableLike()
.ExcludeZeroes();

IList list = session.CreateCriteria( myPerson.GetType() )
.Add( example )
.SetMaxResults( start )
.SetFirstResult( max )
.List();

....



The code limits the returned results by the use of the methods "SetMaxResults" and "SetFirstResult" (I implemented a kind of paging)

Additonally I want to know the total amount of objects returned, without the use of the methods "SetMaxResults" and "SetFirstResult".

Is it possible to query for this amount without receiving all the objects?

Thank you for your answers

SunaX


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 23, 2007 10:18 am 
Regular
Regular

Joined: Thu Nov 23, 2006 10:29 am
Posts: 106
Location: Belgium
Hello,

You can use Projections to get a rowcount of an ICriteria :

Code:
ISession session = EntityMan.DataStore.GetSession();
ICriteria crit = session.CreateCriteria(typeof(Entity));
crit.Projection = Projections.RowCount();
int count = (int)crit.UniqueResult();

MessageBox.Show(count.ToString());

_________________
Please rate this post if it helped.

X.


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 24, 2007 11:07 am 
Newbie

Joined: Wed May 23, 2007 9:14 am
Posts: 2
thank you xasp

that is exactly what i was looking for :-)


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.