How to set formula in grails domain class?

帅比萌擦擦* 提交于 2019-12-22 13:03:08

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/23556773/how-to-set-formula-in-grails-domain-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!