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.  [ 5 posts ] 
Author Message
 Post subject: Projections Examples
PostPosted: Tue Dec 12, 2006 12:28 am 
Regular
Regular

Joined: Wed Oct 25, 2006 10:51 pm
Posts: 71
Projections

I have read the documentation for 1.2beta2-Projections, and there are a few examples but they never use the value returned from the query.

What exactly is being returned, eg in the results list below, as an IList? I don't really understand why it doesn't use generics.

IList results = session.CreateCriteria(typeof(Cat))
.SetProjection( Projections.RowCount() )
.Add( Expression.Eq("Color", Color.BLACK) )
.List();

But the obvious question is, how do I find out how many rows are returned after running this query?
And how we do change this query to return a list of Cat's... ?
I run this on a table that has no records, and I get an IList with a Count of 1. Which makes it perplexing.

I intuitively thought that the IList returned would contain the RowCount... maybe my intuition is crap?

Can anyone provide an example, using Projections, that uses the return value in something realistic, like getting the RowCount and using it?


Top
 Profile  
 
 Post subject: Re: Projections Examples
PostPosted: Tue Dec 12, 2006 3:33 am 
Expert
Expert

Joined: Thu Jan 19, 2006 4:29 pm
Posts: 348
PandaWood wrote:
I run this on a table that has no records, and I get an IList with a Count of 1.


I would think ity is correct: one element in side list, which stores the returned value (0 in You example).

But I do not understand, how You intend to cast the rowcount into cat?

Gert

_________________
If a reply helps You, rate it!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 12, 2006 3:52 am 
Regular
Regular

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

I think you'd better use UniqueResult to retrieve just one piece of info.

Example:
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());


X.

Please rate this post if it helped.


Top
 Profile  
 
 Post subject: Re: Projections Examples
PostPosted: Tue Dec 12, 2006 6:45 pm 
Regular
Regular

Joined: Wed Oct 25, 2006 10:51 pm
Posts: 71
gert wrote:
I would think ity is correct: one element in side list, which stores the returned value (0 in You example).
But I do not understand, how You intend to cast the rowcount into cat?
Gert

Thanks, yeah the confusion for me, is in the word "stores". How or what does it store? I think the people who write the doco, assume you already know, somehow. So, I don't have to or particularly want to cast the rowcount into Cat, I just wanted to understand what this IList returned. Was it Cats or int's or what?
Looking back after seeing the answer, it's because Hibernate is so generically 'capable' that specific tasks are not so obvious. I have to call UniqueResult() on the return value and cast it to an int.


Last edited by PandaWood on Tue Dec 12, 2006 6:53 pm, edited 2 times in total.

Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 12, 2006 6:47 pm 
Regular
Regular

Joined: Wed Oct 25, 2006 10:51 pm
Posts: 71
xasp wrote:
MessageBox.Show(count.ToString());
[/code]


Thanks, that answers it beatifully. That is definitely the answer 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.  [ 5 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.