I need a help with what I believe is very trivial stuff.
class User {
String name
static hasMany = [files : File]
}
class File {
long size
}
Now when I need the total size of all files a given user has I use the following which is working :
def user = User.get(id)
user.files.each(total+=it.size)
but how ugly is it, when I am sure it can be done with simple select sum query with either plain SQL or GORM/CRITERIA
I have tried something like File.sumBySize..()
Or
def c = File.createCriteria()
def f = c.list{
eq("user", user) // What here ?
projections
{
sum("size")
}
}
I dont know how to specify the parent(user) relationship which is not defined in File class but in Grails join tables
Any help appreciated
You can do it with an HQL query, but it has a small problem with the name of the ‘size’ field
(appears to be a reserved word). If you rename it to ‘length’, ‘fileSize’, etc. then this will work: