3

I am trying to write formula in my domain class which helps me in creating criteria.

class MyClass {
    //some fields
    Date appointmentTime
    String ddmmyy
    int year
    int month
    int day
    static transients = [
        'ddmmyy',
        'year',
        'month',
        'day'
    ]
    static mapping= {
        ddmmyy formula('DATE_FORMAT(appointmentTime)')
        year formula('YEAR(appointmentTime)')
        month formula('MONTH(appointmentTime)')
        day formula('DAYOFMONTH(appointmentTime)')
    }
}

Whenever I am trying to use this fields in my criteria it throws error i.e. can not resolve property 'ddmmyy' of 'myClass'.

MyCriteria is:

Date myDate = Calender.instance.time

def results = MyClass.createcriteria().list{
    lt('appointmentTime', date+1)
    ge('appointmentTime', date)
    projections {
        groupProperty('ddmmyy')
        count('id')
    }
}

Any idea why I am getting an exception for this?

GreenGiant
  • 4,930
  • 1
  • 46
  • 76
sanghavi7
  • 758
  • 1
  • 15
  • 38
  • Note that the formula expressed in the ORM DSL is SQL so references to other properties should relate to the persistence model not the object model, which is why the example is wrong being that it refers to appointmentTime and not APPOINTMENT_TIME – Tyler Rafferty Feb 22 '18 at 00:19

1 Answers1

5

You need to make these fields non transient to use in criteria. See reference document

http://gorm.grails.org/6.1.x/hibernate/manual/#derivedProperties

Hussain Fakhruddin
  • 3,202
  • 4
  • 25
  • 36