dslevine wrote:
did you ever find out the correct syntax for this?
I didn't manage to find a way to ORDER BY a projection criteria query, nor did I find any postings about it. I gave up and am using HQL.
My code (below) causes the error: org.hibernate.QueryException: could not resolve property: rval of: com.skahasoftware.Ad. This seems to indicate that Order cannot be used with an SQL function.
Criteria crit = session.createCriteria(Ad.class);
crit.setProjection(
Projections.projectionList()
.add(Projections.sqlProjection("{alias}.id theObj",
new String[]{"theObj"},
new org.hibernate.type.Type[]{Hibernate.entity(Ad.class)}))
.add(
Projections.sqlProjection("rand() as rval",
new String[] {"rval"},
new org.hibernate.type.Type[] {new org.hibernate.type.FloatType()}
)
)
);
crit.addOrder(Order.asc("rval"));