-->
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: Basis createQuery problem
PostPosted: Fri Nov 11, 2005 4:42 pm 
Beginner
Beginner

Joined: Fri Nov 11, 2005 4:35 pm
Posts: 23
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version:3

Hi,

I try to create some basis SQL statements with the createQuery method.

-------- code --------------
Query countQuery = HibernateUtil.currentSession()
.createQuery("select count(*) from (select s.id as id, s.code as code, s.name as name from Service s) as tblCount");

------- Error ---------------

countQuery: select count(*) from (select s.id as id, s.code as code, s.name as name from Service s) as tblCount
11-nov-2005 21:28:55 org.hibernate.hql.ast.ErrorCounter reportError
SEVERE: *** ERROR: line 1:22: unexpected token: (

------ Test in MySQL -------
query: select count(*) from (select s.id as id, s.code as code, s.name as name from Service s) tblCount;

!!!! Same query except I remove the " as " for my table name

result: count(*) = 6


You see the query works in MySQL but not with the HSQL, somebody know where i'm wrong.

Thx in adv,
Pieter Pareit


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 11, 2005 5:15 pm 
Expert
Expert

Joined: Fri Aug 19, 2005 2:11 pm
Posts: 628
Location: Cincinnati
createQuery in hibernate is looking for HQL. to use SQL use the createSqlQuery() method

_________________
Chris

If you were at work doing this voluntarily, imagine what you'd want to see to answer a question.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 11, 2005 7:01 pm 
Newbie

Joined: Mon Oct 03, 2005 8:27 am
Posts: 13
I suggest you this two links: http://www.hibernate.org/hib_docs/v3/reference/en/html_single/#queryhql-subqueries and http://www.hibernate.org/hib_docs/v3/reference/en/html_single/#querycriteria-detachedqueries.

Hi !!!


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 12, 2005 6:00 am 
Beginner
Beginner

Joined: Fri Nov 11, 2005 4:35 pm
Posts: 23
Thx for the replies but it is not what i'm looking for.

I want to use HSQL! And also a subquery in my from clause.

select count(*)
from (
select s.id as id, s.code as code, s.name as name
from Service s
) as tblCount


But that doesn't work...

Grtz,
Pieter


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 12, 2005 9:19 pm 
Beginner
Beginner

Joined: Tue Oct 18, 2005 3:57 pm
Posts: 48
Location: Los Angeles, CA
ppareit wrote:
select count(*)
from (
select s.id as id, s.code as code, s.name as name
from Service s
) as tblCount


Do you really need a subquery for this? If all you're doing is counting rows in table Service, why can't you do

Code:
select count(*) from Service s

???


Top
 Profile  
 
 Post subject:
PostPosted: Sun Nov 13, 2005 8:16 am 
Beginner
Beginner

Joined: Fri Nov 11, 2005 4:35 pm
Posts: 23
Yeah, I know it looks stupid. But I'm creating a framework, and the framework creates a html navigatable table (next, previous).

I send the following query to my framework classes:

select s.id as id, s.code as code, s.name as name
from Service s

But i also want to display the record count, this is why I want to use this query.

select count(*)
from (
select s.id as id, s.code as code, s.name as name
from Service s
) as tblCount

I searched this forum and i found that it is
impossible. Because the subquery creates a list and not a resultset. I solved my problem by changing my framework in HTMLTable.addQuery() and HTMLTable.addCounterQuery().

Thanks for your help!
Pieter


Top
 Profile  
 
 Post subject:
PostPosted: Sun Nov 13, 2005 11:35 am 
Expert
Expert

Joined: Sat Jun 12, 2004 4:49 pm
Posts: 915
i think that hibernate hql don't support subquery in from clause


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.