-->
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.  [ 6 posts ] 
Author Message
 Post subject: Grouped SQL Query
PostPosted: Mon Feb 16, 2004 9:25 am 
Newbie

Joined: Mon Feb 16, 2004 9:19 am
Posts: 11
Hi,

i have a problem creating a SQL Query containing COUNT(*) values:

Code:
_query =
   "select {c.*}, " +
   "count(distinct task.task_id) as {tcount}, " +
   "count(distinct ih.issue_id) as {ihcount}, " +
   "count(distinct im.issue_id) as {imcount} " +
   "from coordinator {c} " +
   "left outer join task on c.coordinator_id=task.responsible_id and (task.status=0 or task.status=1) " +
   "left outer join issue ih on c.coordinator_id = ih.coordinator_id and ih.risk='High' and (ih.status=0 or ih.status=2) " +
   "left outer join issue im on c.coordinator_id = im.coordinator_id and im.risk='Medium' and (im.status=0 or im.status=2) " +
   "group by  c.coordinator_id " +
   "order by  c.name asc";
Query query = _session.createSQLQuery(
   _query,
   new String[] { "c", "tcount", "ihcount", "imcount" },
   new Class[] { Coordinator.class, Integer.class, Integer.class, Integer.class });
return query.list().iterator();


This code does not work, because it results in "No Persister for class Integer".

Regards,
Ralf


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 16, 2004 9:38 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
createSQLQuery can only return Persistent classes, not Integers or other unmapped classes.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 16, 2004 10:12 am 
Newbie

Joined: Mon Feb 16, 2004 9:19 am
Posts: 11
gloeglm wrote:
createSQLQuery can only return Persistent classes, not Integers or other unmapped classes.


Is there any alternative? I tried to use HQL instead but got different answers:

Code:
select   coordinator,
   count(task.taskId),
   count(ih.issueId),
   count(im.issueId)
from   Coordinator as coordinator
left join   coordinator.tasks task
left join   coordinator.issues as ih " +
left join   coordinator.issues as im " +
where   (task.taskId is null
   or task.status = de.reswi.dbats.domain.TaskStatus.OPEN
   or task.status = de.reswi.dbats.domain.TaskStatus.INWORK)
and   (ih.issueId is null
   or (ih.risk = 'High'
   and (ih.status = de.reswi.dbats.domain.IssueStatus.OPEN
   or ih.status = de.reswi.dbats.domain.IssueStatus.RISK_TEMP)))
and   (im.issueId is null
   or (im.risk = 'Medium'
   and (im.status = de.reswi.dbats.domain.IssueStatus.OPEN
   or im.status = de.reswi.dbats.domain.IssueStatus.RISK_TEMP)))
group by   coordinator
order by   coordinator.name asc


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 16, 2004 10:20 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
You are using a left join in your HQL and a left outer join in your SQL, so its clear you get different results, use a left outer join in HQL, too. Enable SQL logging and check the generated SQL.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 16, 2004 11:24 am 
Newbie

Joined: Mon Feb 16, 2004 9:19 am
Posts: 11
gloeglm wrote:
You are using a left join in your HQL and a left outer join in your SQL, so its clear you get different results, use a left outer join in HQL, too. Enable SQL logging and check the generated SQL.


left join is a synonym to left outer join.

The cause for different results are the condition in the WHERE clause. These conditions should be part of the SQL join statement to get indenpendent results. The additional criterias in the WHERE statement lead to dependent (and wrong) results.

is there any chance to do something like

SELECT x FROM y
LEFT JOIN z as z z.x = ?
WHERE ....


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 16, 2004 11:29 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Oh my, I am really not fully functional today :) But well, arbitary join conditions are not supported so far. I think it is a feature which might be included in future versions.


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