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: CreateSQLQuery SQL and scalar
PostPosted: Tue Oct 03, 2006 7:33 am 
Newbie

Joined: Tue Oct 03, 2006 7:25 am
Posts: 8
I was trying to retrieve COUNT(*) of rows by using CreateSQLQuery but realised that it's not possible at this moment:

session.CreateSQLQuery(@"SELECT count(*) AS {r.id} FROM Rez {r} WHERE f_AgeInDays({r}.Create_Date, 'NOW') <= 7", "Rez", typeof(Rez));

It seems that type and alias are obligate parameters - is it possible to somehow workaround it?

Version: NHibernate-1.2.0.Beta1-debug


Thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 03, 2006 2:42 pm 
Beginner
Beginner

Joined: Sat Sep 09, 2006 5:55 am
Posts: 23
It may work better if you bind 'r' to typeof(Rez), since you use 'r' to identify it between the {}'s:
Code:
session.CreateSQLQuery(@"SELECT count(*) AS {r.id} FROM Rez {r} WHERE f_AgeInDays({r}.Create_Date, 'NOW') <= 7", "r", typeof(Rez));

I don't know if you really want to use the id property for this; you might want to check out the new scalar query feature for native sql. It's under "13.1.1. Scalar queries" in the documentation located in the doc folder of your nhibernate installation.

You'd probably end up with something like:
Code:
sess.CreateSQLQuery(@"SELECT count(*) as REZ_COUNT FROM Rez WHERE f_AgeInDays(Rez.Create_Date, 'NOW') <= 7")
.AddScalar("REZ_COUNT", NHibernateUtil.Int64);

An IList will be returned containing the scalar values (they might be wrapped in object arrays, but I'm not sure).

Hope this helps.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 03, 2006 4:47 pm 
Senior
Senior

Joined: Mon Aug 21, 2006 9:18 am
Posts: 179
Look into the new Projection Criteria that is part of NHib 1.2 beta 1. It has things like Projections.Count(propertyName) and when combined with Expression calls will do what you are after.

_________________
If this helped...please remember to rate it!


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.