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

Вопрос

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