-->
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: Need help with HQL to find max collection size
PostPosted: Mon Sep 17, 2007 1:49 pm 
Newbie

Joined: Mon Sep 17, 2007 1:28 pm
Posts: 6
Hi,

This has been a lot more challenging than I thought, hopefully I am missing something. I have two classes related via a @ManyToMany - physician, and site. I need to query the physician table to find out how many sites someone has at most.

Here is the code for the relationships, site is the owning side.

Code:
public class Physician extends IdEntity<Short> {

   private List<Site> siteList;
      @ManyToMany(mappedBy="physicianList")
      public List<Site> getSiteList() {
         return siteList;
      }
}


public class Site extends IdEntity<Short> {

   private List<Physician> physicianList;
      @ManyToMany(cascade=CascadeType.ALL)
      public List<Physician> getPhysicianList() {
         return physicianList;
      }
}



in native sql i can just do

Code:
select max(counts.theCount) as maxCount from (SELECT count(*) as theCount FROM [tbl_Site_Physician] group by physicianList_id) counts


notice the nested select, hql doesn't go for that apparently. I am able to do this in hql:

Code:
select count(siteList) from Physician as phy left join phy.siteList as siteList group by phy


which gives me a list of the size of each phy's site list, like
4
3
5
7
8
2

out of that list i need the max, so you would think

Code:
select max(counts.theCount) from (select count(siteList) as theCounts from Physician as phy left join phy.siteList as siteList group by phy) theCounts


but I just get a syntax error everytime on the ( , which means this isn't supported or I am just flat out screwing up.

In the meantime, I am getting by with that query order by theCount desc .setMaxResults(1) .uniqueResult() which works, but still I'd like to know the better way

TIA[/quote]


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 17, 2007 2:03 pm 
Regular
Regular

Joined: Sun Sep 17, 2006 2:48 am
Posts: 81
Location: California
Just wondering if max in the same query might work? (Just a guess)

select max(count(siteList)) from Physician as phy left join phy.siteList as siteList group by phy

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 17, 2007 2:04 pm 
Newbie

Joined: Mon Sep 17, 2007 1:28 pm
Posts: 6
unfortunately that was the first thing I tried. I'm not sure if this is something on SQL servers end, it may work with other RDBMSs.

Msg 130, Level 15, State 1, Line 1
Cannot perform an aggregate function on an expression containing an aggregate or a subquery.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 18, 2007 5:45 am 
Regular
Regular

Joined: Tue May 16, 2006 3:32 am
Posts: 117
Quote:
HQL subqueries may occur only in the select or where clauses. - Hibernate reference doc.


Try rewriting using a different logic or use native sql.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 18, 2007 8:10 am 
Beginner
Beginner

Joined: Fri Sep 08, 2006 7:29 am
Posts: 36
Try using Projections and Aggregations


Code:
Criteria criteria = session.createCriteria(xyz.class);
criteria.setProjection(Projections.rowCount());
List results = criteria.list();


The results list will contain one object, an Integer that contains the results of executing the COUNT SQL statement.

Group By

Code:
Criteria criteria = session.createCriteria(xyz.class);
ProjectionList projectionList = Projections.projectionList();
projectionList.add(Projections.groupProperty("x"));
projectionList.add(Projections.groupProperty("y"));
criteria.setProjection(projList);
List results = criteria.list();



You would have to work a little on it...


Top
 Profile  
 
 Post subject: test
PostPosted: Tue Sep 18, 2007 9:33 am 
Newbie

Joined: Mon Sep 17, 2007 1:28 pm
Posts: 6
thanks guys, i have been working on it. No luck with the criteria api yet, i'll give it another shot.



HQL subqueries may occur only in the select or where clauses. - Hibernate reference doc.


That's a pretty big limitation of HQL isn't it?? I'm sure there's a reason for it but that has to be a very commonly asked for feature yes?

Rewriting using different logic is the idea, i just need help. Native sql is out of the question, this is actually one example of a generic routine to (de)serialize a database to and from plaintext.

Currently I have

Code:
   private <E extends Entity> Long queryMaxCollectionSize(Class<E> clazz,
         String prop, Manager<E> manager) {
      return (Long) manager
            .newQuery(
                  "select count(collection) from "
                        + clazz.getName()
                        + " as clazz left join clazz."
                        + prop
                        + " as collection group by clazz order by count(collection) desc")
            .setMaxResults(1).uniqueResult();
   }


and it is working fine, just looking for that better solution without the order by and maxresult


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.