let’s say i have this classes which are being mapped to SQLServer:
class Statistic {
Servicos servico
int totalTime
Date date
static constraints = {
}
}
class Servicos {
String name
String description
static constraints = {
}
}
Using SQL Queries like this:
select name, description, media,frequencia from TvMagazinePlus.dbo.servicos as t1 join (
SELECT TOP 1000
[servico_id]
,avg([total_time]) as 'media'
,COUNT([servico_id]) as 'frequencia'
FROM [TvMagazinePlus].[dbo].[statistic] where date between now and yesterday group by [servico_id]) as t2 on t1.id = t2.servico_id
I get this table:

I now need to render the result in JSON. My problem is, i find executeQuery too difficult to accomplish this. Is there any way i can do this using HQL like createCriteria?
You could easily set that result in a list of beans and then serialize them in JSON with any framework like Jackson or anything…
I don’t know grails but with Hibernate you can use criteria to execute subqueries, according to the hibernate documentation:
http://docs.jboss.org/hibernate/core/3.3/reference/en/html/querycriteria.html
This obviously return the cats that have a weight greater than the average cat’ weight, using a subquery to get that average cat’s weight.
To map the result into an XML/JSON serializable bean you can simply use a AliasToBeanResultTransformer on that criteria and then serialize your objects 😉