When using criteria queries, many times we encounter a use case where we need to order our result not on any field of domain class but on our result set, in these cases aliases can be used.


In my project, use case was to find most liked products from the domain:

ProductStat{
Date dateCreated
Product product
}

To find the result, the query I used was :

ProductStat.createCriteria().list() {
            projections {
                groupProperty("product")
                count("product", "totalProducts")  // alias totalProducts
            }
            order("totalProducts", "desc")
}

In this way using alias “totalProducts” I was able to order the result according to the count of products.


							
		
puneet

Aseem BansalGaurav SharmaDivya Setia
Subscribe to our Blog

Get latest articles straight to your inbox. Enter your email address below: