hanaZ
hanaZ

Reputation: 261

GORM for MongoDB: Derived Property?

I am trying to build a criteria of comparing the value of a derived property of adding two fields as shown in Can I do a math operation inside a createCriteria, i.e.

class SumFormula {
    Integer column1
    Integer column2
    Integer sum

    static mapping = {
       sum formula: 'column1 + column2'
    }
}

and the criteria:

SumFormula.createCriteria().list() {
    ne("sum", 100)
}

However I could not make it work with MongoDB. The derived property is always null when printed out.

The above cited post did mention that derived properties are SQL expressions, so the questions is that are the derived properties only available with GORM for SQL? Any alternative for GORM for MongoDB?

Upvotes: 1

Views: 113

Answers (2)

Matthias Born
Matthias Born

Reputation: 21

We are facing the same problem. Currently, our workaround is to actually store the derived properties in MongoDB and use the beforeUpdate methods to calculate the values.

def beforeUpdate() {
    sum = column1 + column2
}

Upvotes: 0

Graeme Rocher
Graeme Rocher

Reputation: 7985

derived properties are a Hibernate/SQL specific feature and are not supported in GORM for MongoDB. An alternative is to simply do this in code:

class SumFormula {
   Integer column1
   Integer column2
   Integer getSum() { column1 + column2 }

}

Upvotes: 1

Related Questions