Question

For instance, suppose I wanted to let that column be set to whatever the database defaults it to, without redefining that default in the domain class?

I can't find much through Google. There are hints that if I were working with Hibernate directly, I could set that particular column/property to private, and this might accomplish what I seek.

I can of course leave that column undefined, and GORM ignores it. But I need the values out of it whenever the Grails app does a select.

Was it helpful?

Solution

You can use the GORM property insertable as in doc or can read the value with a beforeInsert event:

class Book {
    String title
    String isbn

    static mapping = {
        isbn nullable: false
    }

    def beforeInsert {
        title = queryFromDatabase...
    }
}

OTHER TIPS

I think you have to go the beforeInsert / Hibernate interceptor route since your requirement is to read default values from an existing database.

You can read the database default values for columns with JDBC's DatabaseMetaData.getColumns .

To find out the database table and column names, you can use something like this (this code is not tested)

import org.codehaus.groovy.grails.orm.hibernate.cfg.GrailsDomainBinder
import org.codehaus.groovy.grails.orm.hibernate.cfg.Mapping
import org.codehaus.groovy.grails.commons.DomainClassArtefactHandler

def gdc=grailsApplication.getArtefact(DomainClassArtefactHandler.TYPE, someInstance.class.name)
Mapping mapping=GrailsDomainBinder.getMapping(gdc)
def tableName=mapping.tableName
def columnName=mapping.getPropertyConfig('someColumn').column

This is not a complete answer, but I hope this helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top