-->
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: DetachedCriteria Group By with Count restriction - possible?
PostPosted: Thu Jul 19, 2007 3:10 pm 
Regular
Regular

Joined: Sat Jan 22, 2005 6:57 pm
Posts: 50
Location: Chicago
I am facing a problem where I am trying to do a group by and count unique and display only those records where the count is at least some number. I was able to successfuly write some HSQL below that will return me the count and an instance, but I want to further reduce the resultset by saying that I only want to show those where the count > x. Below is the hsql.

Code:
      StringBuffer sql = new StringBuffer();
      sql.append("select count(lmdv.levelInstance.id) as zcount, lmdv.levelInstance.id as zid from LevelMetaDataValue lmdv where (");
      sql.append("(lmdv.typeName = ? and lmdv.value = ?)");
      sql.append("OR (lmdv.typeName = ? and lmdv.value = ?)");
      sql.append(") group by lmdv.levelInstance.id");
      Query query = session.createQuery(sql.toString());


So then I attempted to do it not using HSQL and instead used criteria and detached criteria like so:

Code:
      DetachedCriteria detachedCriteria = DetachedCriteria.forClass(LevelMetaDataValue.class);
      Criterion metadata1 = Restrictions.and(Restrictions.eq("typeName", "manuals"), Restrictions.eq("value", "0"));
      Criterion metadata2 = Restrictions.and(Restrictions.eq("typeName", "imageCount"), Restrictions.eq("value", "0"));
      detachedCriteria.add(Restrictions.or(metadata1, metadata2));
      detachedCriteria.setProjection(Projections.alias(Projections.count("levelInstance.id"), "zcount"));
      detachedCriteria.setProjection(Projections.groupProperty("levelInstance.id"));


However I am still at a loss on how to say only show me those results where the count is > x.

Does anyone know how to do this? Do I need some sort of subselect that checks on count? I have tried to do that unsuccessfully. I am using Oracle as the DB.

Thank you in advance,
Mark


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.