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!