Search code examples
grailsgroovygrails-orm

Result from database function query to Gorm field


In Grails/Groovy, I have defined database fields in Model, I'm using MyModel.createCriteria().list(...) to get query result - works fine!

But in addition to fields, I need value from database function. SQL analog would be:

SELECT foo, bar, calculate_stuff(foo) FROM baz;

Can I include result from DB function call to Domain as transient value?


Solution

  • You might be able to accomplish this using the formula of the mapping for this property. This is known as a derived property. You can read more about it in the documentation.

    For example:

    class Baz {
      String foo
      String bar
      Long thing
    
      mapping {
        thing formula: "calculate_stuff(foo)"
      }
    
    }