-->
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.  [ 2 posts ] 
Author Message
 Post subject: Query to get max size of a persistent List object
PostPosted: Wed Aug 08, 2012 12:45 pm 
Newbie

Joined: Wed Sep 16, 2009 11:06 am
Posts: 7
Ok, I've been searching all over to try to figure this out without any luck.

What I'm trying to do is run a query that will return the maximum java.util.List.size of each distinct root entity. Allow me to demonstrate using a shortened version of my parent class:

Code:
public class CustomSpecRecord
{
   @Id
   @GeneratedValue(strategy = GenerationType.AUTO)
   private Long id;

   ...

   @CollectionOfElements(targetElement = java.lang.String.class)
   @JoinTable(name = "CustomSpecAlias", joinColumns = @JoinColumn(name = "RecordId"))
   @IndexColumn(name = "AliasNumber", base = 1)
   @Column(name = "Alias", nullable = false)
   private List<String> aliases;

}


What I need is a query that will return the equivalent of "max(aliases.getSize())". I would most strongly prefer a Criteria query, if necessary an HQL query. I know I can do this in raw SQL if absolutely necessary, but that kind of defeats the point of using Hibernate :)

I've tried this, but it doesn't work, says "aliasCount" is an unknown property:
(I am running this inside Spring so please ignore spring-related stuff unless it's applicable)

Code:
   public int getMaxAliasCount()
   {
      DetachedCriteria dc = DetachedCriteria.forClass(CustomSpecRecord.class);

      dc.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
      ProjectionList projList = Projections.projectionList();
      projList.add(Projections.count("aliases"), "aliasCount");
      projList.add(Projections.max("aliasCount"), "maxAliasCount");
      dc.setProjection(projList);

      return (Integer) getHibernateTemplate().findByCriteria(dc).get(0);
   }


I'm using Hibernate 3.2.7.ga on Java 5 (don't have the option to upgrade at this time), and Annotations 3.4.0.GA
Thanks in advance!


Top
 Profile  
 
 Post subject: Re: Query to get max size of a persistent List object
PostPosted: Fri Aug 10, 2012 4:22 pm 
Newbie

Joined: Wed Sep 16, 2009 11:06 am
Posts: 7
Never mind guys. I just decided to add a property to my object to contain the aliasCount and do a Projections.max("aliasCount") in my criteria query. A bit of a hack but it works


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