-->
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.  [ 7 posts ] 
Author Message
 Post subject: How do I transform this CreateSQLQuery to get a count?
PostPosted: Wed Feb 08, 2006 3:31 pm 
Newbie

Joined: Wed Dec 14, 2005 3:29 pm
Posts: 16
I am doing this:

Code:
                IQuery query = session.CreateSQLQuery(
                        "SELECT {DiscussionPost.*} FROM DiscussionPost {DiscussionPost}" +
                        " WHERE FREETEXT ((Body, Subject), '" + Renderer.SqlEncode(s) + "')",
                        "DiscussionPost",
                        typeof(DiscussionPost));


How do I transform this into a COUNT() query? (I just want the number of items returned without calling .List()). I tried a bunch of ways at no avail ...

Also, FREETEXT doesn't seem to work with CreateQuery(), but I think it should since it's just a WHERE statement, specific to MSSQL.

Thx
-dB.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 08, 2006 4:24 pm 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
Well if you don't need the objects themselves, then you don't really need NHibernate. Just get the connection from a session and execute your command directly.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 08, 2006 4:33 pm 
Newbie

Joined: Wed Dec 14, 2005 3:29 pm
Posts: 16
sergey wrote:
Well if you don't need the objects themselves, then you don't really need NHibernate. Just get the connection from a session and execute your command directly.


This is possible in theory, but is becoming a really bad idea in practice.

It's clearly a shortcut in terms of development practices and I don't want to encourage using the SQL connection directly since the entire application standardizes on NH. We do COUNT elsewhere in an NH query. Also, the connection object is not accessible in the context on purpose, so I don't want to abandon that easily :)

In addition, I am hoping that one day NHibernate can, for example, do a great job caching objects returned from a FREETEXT query. Then I'll ask NH the count instead of SQL.

So is this impossible right now?

Much appreciated,
dB.


Top
 Profile  
 
 Post subject: i have the same problem
PostPosted: Wed Feb 22, 2006 11:57 pm 
Newbie

Joined: Thu Dec 22, 2005 4:50 am
Posts: 10
I got the same problem. I cannot create a new transaction with the connection in Nhibernate (using SQL Server), and dont really want to interrupt the session. and I do not want to do it as suggested above.

Why will it only allow to return class types?

Can i get something like below to work?


Code:
sb.Append("select count(*) from 'table'  etc ......

IQuery q = theSession.CreateSQLQuery(sb.ToString(), new string[]{"alias"}, new Type[]{typeof(int)});


Top
 Profile  
 
 Post subject: problem
PostPosted: Thu Feb 23, 2006 12:23 am 
Newbie

Joined: Thu Dec 22, 2005 4:50 am
Posts: 10
sergey, im tryign what you suggested, getting the current connection. However it says that I cannot execute my transaction because hibernate is in the middle of pending transaction.

I tried to do a flush of the session but that didnt work.

Since im using sql server i cannot open another transaction when one is pending.

Any ideas? For now i have to try creating a brand new session which i do not want to have to do


Top
 Profile  
 
 Post subject: Count
PostPosted: Thu Feb 23, 2006 2:59 am 
Regular
Regular

Joined: Tue Jan 03, 2006 7:21 am
Posts: 85
If you just want the count of an entity you can do something like this:

Code:
IQuery q = session.CreateQuery("select count(c) from Country c");
            Console.WriteLine(q.UniqueResult().ToString());


It results in the following query to be fired:
NHibernate :select count(country0_.id) as x0_0_ from Country country0_

and I get the output as 10

please note that Country is a class. This works. Is this what you are looking for.


Top
 Profile  
 
 Post subject: Add Count property in ICriteria (JIRA NH-307)
PostPosted: Fri Feb 24, 2006 7:42 pm 
Newbie

Joined: Tue Jan 31, 2006 2:29 pm
Posts: 12
This isn't what you find but can help you ...

See the link:

http://jira.nhibernate.org/browse/NH-307?page=all

You will add Count property in ICriteria and it will to execute a select count in database ...


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