I am using a named query with projections in my domain class. I would like the named query to return one of the values using the following equation:
sum(x * y) / sum(y)
I am familiar with formulas in mapping and attempted to do the following:
def formVar
static mapping = {
formulaVar formula: 'sum(x * y) / sum(y)'
}
Any help here would be awesome.
I might be misunderstanding your question, but you may want to check out the beforeUpdate and beforeInsert events.
Code placed in this block will allow you to preform calculations and manipulate the domain class properties before inserting/updating them.
It is documented in the Grails manual here http://grails.org/doc/2.3.7/guide/GORM.html#eventsAutoTimestamping
OK. Got this working like this:
class SumThing {
Double sumVar
static namedQueries = {
sums { op ->
projections {
property "sumVar"
}
}
}
static mapping = {
version false
columns {
sumVar formula: 'SUM(X * Y) / SUM(Y)'//X and Y being actual column names from your database
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.