I want to create a Hibernate Criteria for the following SQL statement:
Code:
select upper(left(name,1)) as x, count(*) from products group by x order by x desc;
I tried this code:
Code:
HibernateUtil.currentSession()
.createCriteria(ProductDB.class)
.setProjection(Projections.projectionList()
.add(Projections.sqlGroupProjection(
"upper(left(name, " + (prefix.length() + 1) + ")) as x",
"x", new String[] { "x" }, new Type[] { Hibernate.STRING }))
.add(Projections.rowCount()))
.addOrder(Order.desc("x"));
But it's not working because:
Code:
org.hibernate.QueryException: could not resolve property: x of: Product
Why is Hibernate searching for x in product instead of using the alias x introduced by sqlGroupProjection?
There are work-arounds, for example sorting the result list with Collections.sort or using createSQLQuery.
But is it possible to do it with createCriteria?
Or is sqlGroupProjection not enough and we need something like a sqlGroup
OrderProjection?