-->
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: hibernate subqueries: group by average
PostPosted: Mon Oct 03, 2011 7:32 am 
Newbie

Joined: Mon Oct 03, 2011 7:15 am
Posts: 1
Hey there,
I have the the following problem: the basic idea is to fetch sorted list of objects (from tableA) ordered by the price average of corresponding items from tableB (tableA 1:n TableB).
i.e. tableA has multiple items, each of which has multiple prices associated, recorded on tableB.
The SQL (below) works fine, but I'm struggling to get the same results using d criteria api.

Code:
--    **tableA**
--    tableA_id (primary)
--    names
--    ...
--
--    tableA (1:n) tableB
--
--    **tableB**
--    tableB_id
--    tableA_id (fk)
--    price

select * from tableA A
inner join
        (
        select tableA_id, avg(price) as avgPrice
        from tableB group by tableA_id
        ) B
on
  A.id = B.id
ORDER BY B.avgPrice


so far I tried different solution, none of them really worked. i.e.:


Code:
ProjectionList projList = Projections.projectionList();
   projList.add(Projections.avg("tableB.price").as("tableB.avgPrice"));
   projList.add(Projections.groupProperty("tb.tableB"));


DetachedCriteria dcrit = DetachedCriteria.forClass(TableB.class,"tb")
                  .setProjection(projList)

Criteria cg = getEntityManager().createQuery(TableA.class)
                  .createCriteria("TableB", "tb")
                  .add(Subqueries.exists(dcrit))
          .addOrder(Order.asc("tb.avgPrice"));


The result is:
Code:
Cause: could not resolve property: avgPrice of: TableB


Any idea???


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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.