-->
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.  [ 5 posts ] 
Author Message
 Post subject: Criteria API: Add a count subquery
PostPosted: Mon Mar 06, 2006 6:23 am 
Regular
Regular

Joined: Mon Mar 06, 2006 6:18 am
Posts: 95
Location: Bern, Switzerland
Hi all

i have the following SQL Query:

SELECT DISTINCT TKAG.ILAUFNUMMER, TKAG.SKURSNR, TKAG.SREGION, TKAG.DKURSDATUM,
(SELECT count(*) FROM TKURSAGTEIL TKTE WHERE TKTE.FK_TKAG_LAUFNR = TKAG.ILAUFNUMMER) AS TEILE
FROM TKURSANGEBOT TKAG
WHERE ........

I was able to add Restrictions in the WHERE clause. This works fine. But now i would like to add the count subselect. How do i have to implement this with the Criteria API? Do i have to use DetachedCriteria or Projections?

Any help would be appreciated...

Kind Regards
Angela


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 06, 2006 5:17 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Count is a projection. Projections.count(prop) is what you need. If you want to filter your query based on counts, then you'll need to use DetachedCriteria to wrap the projection into an subquery. Something like
Code:
Criteria crit = ...;
DetachedCritieria countSubquery = DetachedCriteria.forClass(clazz);
countSubquery.setProjection(Projections.count("propToCount"));
crit.add(Subqueries.gt(Integer.valueOf(5), countSubquery));
return crit.list();
That'll get all instances of clazz that have the same "propToCount" as at most 4 other instances. (5 > (select ...))


Top
 Profile  
 
 Post subject: Add a count subquery - V2
PostPosted: Mon Mar 06, 2006 7:40 pm 
Newbie

Joined: Tue Apr 19, 2005 5:05 pm
Posts: 3
tenwit wrote:
Count is a projection. Projections.count(prop) is what you need. If you want to filter your query based on counts, then you'll need to use DetachedCriteria to wrap the projection into an subquery. Something like
Code:
Criteria crit = ...;
DetachedCritieria countSubquery = DetachedCriteria.forClass(clazz);
countSubquery.setProjection(Projections.count("propToCount"));
crit.add(Subqueries.gt(Integer.valueOf(5), countSubquery));
return crit.list();
That'll get all instances of clazz that have the same "propToCount" as at most 4 other instances. (5 > (select ...))

Is there any way to accomplish this in version 2?

Would you use these same idioms to handle a counting sub-select within the FROM clause?

e.g.

SELECT a.infoA, b.infoB, c.itemCount
FROM TableA a, TableB b,
(SELECT count(*) FROM TableC WHERE c.infoA_ID) c
WHERE 1=1
AND a.infoA_ID = :qinfoA_ID
AND a.infoA_ID = b.infoA_FK(+)

From what I can tell, there is no way to accomplish either of these in V2 unless you run native SQL through SQLQuery. Am I wrong?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 06, 2006 8:39 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
As far as I'm aware, there's no way to use criteria to put a subselect in the select or from clauses, only in the where clause.

You can use Restrictions.sqlRestrictions in Hibernate 2 or 3 to accomplish the same things as I laid out in my earlier post.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 07, 2006 2:13 am 
Regular
Regular

Joined: Mon Mar 06, 2006 6:18 am
Posts: 95
Location: Bern, Switzerland
I also found another way to solve to problem. I jut put a formula into my hibernate mapping file:

<property name="kursTeileCount" formula="(SELECT count(*) FROM TKURSAGTEIL TKTE WHERE TKTE.FK_TKAG_LAUFNR = ILAUFNUMMER)"/>

Angela


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