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.

有帮助吗?

解决方案 2

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
        }
    }
}

其他提示

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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top