I think you need to go through the beforeInsert / Hibernate interceptor route, since your requirement is to read the default values ββfrom an existing database.
You can read the database defaults for columns using JDBC DatabaseMetaData.getColumns .
To find out the names of the tables and columns of the database, you can use something like this (this code has not been 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.
source share