Is it possible to force Grails/Gorm to not include a column in an insert?

StackOverflow https://stackoverflow.com/questions/12466938

  •  02-07-2021
  •  | 
  •  

سؤال

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.

هل كانت مفيدة؟

المحلول

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...
    }
}

نصائح أخرى

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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top