hibernategrailsgrails-ormgrails-3.3

How to override a DomainClass getter in Grails 3


I'm upgrading a Grails 2 app to 3.3.10 and we have alot of custom getters that aren't being called. Many have fallback logic for nullable values.

Color getFavColor(){
    if(!favColor){
        return "black"
    }

    return favColor
}

Edit: Added an actual method

Vendor getMarketPlaceVendor() {
    if (marketPlaceVendor) {
        return marketPlaceVendor
    }
    return campaign?.marketplaceVendor
}

Stepping into the code it appears that HibernateUtils is accessing the property directly, I can't find anyway to get around this? def propertyValue = reflector.getProperty(thisObject, propertyName)

1) Is there quick fix that I'm missing?

2) What the ideal pattern here? I'm newer to grails and wondering if logic like should be moved to services.

Grails 3.3.10 | Hibernate 5 | Gorm 6.1.12.RELEASE


Solution

  • GORM 6.1 uses field access by default, which means the field is used when reading and writing data via reflection to instances.

    If you wish to continue to use property access this can be configured by altering the default mapping in your configuration:

    import javax.persistence.*
    grails.gorm.default.mapping = {
       '*'(accessType: AccessType.PROPERTY)
    }
    

    That is taken directly from our user guide at http://gorm.grails.org/6.1.x/hibernate/manual/.

    I hope that helps.