-->
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.  [ 1 post ] 
Author Message
 Post subject: Nested Select Count Query with Criteria
PostPosted: Tue Jul 06, 2010 12:11 pm 
Newbie

Joined: Thu Jan 14, 2010 7:30 am
Posts: 2
Hi all,

say that we have 2 entity EntityA and EntityB , with the related tables, TableA and TableB.

i have to implement this query:

Code:
select a.Id , (select count(b.Id) from TableB b where b.FKa = a.Id and b.AnotherField > 0) as TheCount
from TableA a


i'm very close to that since i wrote this code:

Code:
var subCrit = DetachedCriteria.For<EntityB>
                                        .Add<EntityB>(e => e.AnotherField > 0)
                                        .SetProjection(LambdaProjection.Count<EntityB>(e => e.Id).As("TheCount"));

var crit = Session.CreateCriteria<EntityA>
                        .SetProjection(LambdaProjection.GroupProperty<EntityA>(e => e.Id).As("Id),
                                            Projections.SubQuery(subCrit));   


if i execute this criteria i obtain the following SQL:

Code:
select a.Id as Id , (select count(b.Id) from TableB b where b.AnotherField > 0) as TheCount
from TableA a


as u can see , it's very close to what i'm trying to achieve...the problem (and it's definetely a big problem :D)
is that theres no link between the subquery and the entities of TableA ( where b.FKa = a.Id ).
I cant find a way to correlate the subquery to the external query via criteria.

Any suggestions ?

Ta a lot

Alessandro


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.