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

For example, suppose I wanted this column to be configured to which database it is by default without overriding this default value in the domain class?

I can not find much through Google. There are clues that if I worked with Hibernate directly, I could set this particular column / property to private, and that could do what I'm looking for.

I can of course leave this column undefined, and GORM ignores it. But I need values ​​from it whenever a Grails application makes a choice.

+4
source share
2 answers

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

 class Book { String title String isbn static mapping = { isbn nullable: false } def beforeInsert { title = queryFromDatabase... } } 
+2
source

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.

+1
source

Source: https://habr.com/ru/post/1434704/


All Articles