Can I do a math operation inside a createCriteria

2019-09-04 03:00发布

问题:

Is it posible to do a math problem inside a createCriteria?

For example:

If in my table I have two columns, if the two together make 100 I don't want to show it in my result of the query

or

if the other column is in other table?

table

column 1  column 2

  50        50
  20        20

I want the second row

or

table 1       table 2 

  50            50
  20            20

回答1:

One option would be to define a formula field in your domain class. Something like:

class SumFormula {
    Integer column1
    Integer column2
    Integer sum

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

Then, you can apply criteria as follows:

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