-->
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.  [ 11 posts ] 
Author Message
 Post subject: Count(*) in hibernate
PostPosted: Mon Mar 05, 2007 1:54 am 
Newbie

Joined: Fri Mar 31, 2006 7:46 am
Posts: 17
Hi

I have a query.Whenever I execute the query written in my hbm.xml

SELECT count(*)
FROM table tablename

I get, org.hibernate.exception.SQLGrammarException: could not execute query as an Exception.

If, I directly write a query in my persistent Class, then also i get some other exception. Kindly tell me how to use this thing..


Top
 Profile  
 
 Post subject: Re: Count(*) in hibernate
PostPosted: Mon Mar 05, 2007 2:11 am 
Newbie

Joined: Mon Mar 05, 2007 1:39 am
Posts: 10
Location: Brisbane, Australia
pams wrote:
SELECT count(*)
FROM table tablename


Try

Code:
SELECT COUNT(*) FROM tablename


Cheers
mc

_________________
Murray Collingwood
Focus Computing
http://www.focus-computing.com.au


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 05, 2007 2:21 am 
Newbie

Joined: Fri Mar 31, 2006 7:46 am
Posts: 17
Nope, it didnt work....


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 05, 2007 2:28 am 
Newbie

Joined: Fri Mar 31, 2006 7:46 am
Posts: 17
hey,

If I use "SELECT COUNT(*) FROM table" in hbm.xml, I am getting SQLgrammerException.

But, If I directly use it in PersistenceManager, then I am getting "undeclared Throwable Exception"


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 05, 2007 2:49 am 
Newbie

Joined: Mon Mar 05, 2007 1:39 am
Posts: 10
Location: Brisbane, Australia
Can you include more of your hbm.xml - I just want to see the SQL in context.

Cheers
mc

_________________
Murray Collingwood
Focus Computing
http://www.focus-computing.com.au


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 05, 2007 2:56 am 
Newbie

Joined: Fri Mar 31, 2006 7:46 am
Posts: 17
<sql-query name="Querytablename_Keys_operation">
<return alias="tablename" class="path of java file same as tablename"/>
SELECT COUNT(*) FROM TABLE {tablename}
</sql-query>


In hibernate-mapping section, all the columns of that table are mapped to the same java file.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 05, 2007 3:12 am 
Newbie

Joined: Mon Mar 05, 2007 1:39 am
Posts: 10
Location: Brisbane, Australia
pams wrote:
<sql-query name="Querytablename_Keys_operation">
<return alias="tablename" class="path of java file same as tablename"/>
SELECT COUNT(*) FROM TABLE {tablename}
</sql-query>


I just realised the problem...

you have defined the return value as a Person table but the query is going to be returning a number only.

Cheers
mc

_________________
Murray Collingwood
Focus Computing
http://www.focus-computing.com.au


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 05, 2007 3:43 am 
Newbie

Joined: Fri Mar 31, 2006 7:46 am
Posts: 17
yeah.. that is true..

CAn you tell me in detail how should I do it, I mean what excatly should me the entire thing that has to be done in hbm and in java file..


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 05, 2007 3:50 am 
Newbie

Joined: Mon Mar 05, 2007 1:39 am
Posts: 10
Location: Brisbane, Australia
Good question, haven't done this myself, not this way...try something like this.

Code:
<sql-query name="Querytablename_Keys_operation">
<return alias="tablename" class="path of java file same as tablename"/>
SELECT COUNT(*) AS {tablename.recordcount} FROM TABLE {tablename}
</sql-query>



And then in your Java file add:
public void setRecordCount(Integer i) {
this.recordcount = i;
}

Cheers
mc

_________________
Murray Collingwood
Focus Computing
http://www.focus-computing.com.au


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 07, 2007 12:35 am 
Newbie

Joined: Fri Mar 31, 2006 7:46 am
Posts: 17
Hey, I tried that but it didnt work
it gives

java.lang.reflect.UndeclaredThrowableException
at $Proxy0.createQuery(Unknown Source)

as an error.

I tried writing this code in my PersistenceManager class, but the result is the same...


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 07, 2007 1:26 am 
Newbie

Joined: Mon Mar 05, 2007 1:39 am
Posts: 10
Location: Brisbane, Australia
Code:
<sql-query name="Querytablename_Keys_operation">
<return alias="mycount" class="Integer"/>
SELECT COUNT(*) AS {mycount} FROM TABLE Keys
</sql-query>


Okay, I read a bit more. I would try something like this. The return element defines what is being returned. We simply need to get the value being returned into an Integer object. Hence I have changed the return element to reflect this.

You can hardcode the name of the table where I put "Keys". This is no longer the class value.

Also, in your code where you call this query:

Code:
Integer result = (Integer) session.getNamedQuery("Querytablename_Keys_operation").uniqueResult();


Again, I haven't tested this but I'm sure it is along these lines.

Cheers
mc

_________________
Murray Collingwood
Focus Computing
http://www.focus-computing.com.au


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