How do I do more complex calculations when using projections in a named query in GRAILS 2.3.x?

StackOverflow https://stackoverflow.com/questions/23375284

Pergunta

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.

Foi útil?

Solução 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
        }
    }
}

Outras dicas

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

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top