javagroovygrails-orm

Apply application.groovy in GORM standalone


In my script I'm using

implementation 'org.grails:grails-datastore-gorm-hibernate5:8.0.1'

and initialize the domain classes with

Map config = new YamlSlurper().parse( getClass().getResourceAsStream( '/application.yml' ) )
new HibernateDatastore( config.gorm, classes as Class[] )

Now I want to provide some default values to each Entity's mapping as per ref-doc by adding the code to src/main/resources/application.groovy:

grails.gorm.default.mapping = {
  id column:'o_id'
  version column:'chng_id'
  lastUpdated column:'upd'
}

The file is ignored though, and no default mappings are applied.

What is the proper way to apply the default mappings in a GORM standalone setup?


Solution

  • I ended up with the following setup:, putting the GORM default mapping into the initializer class:

    static MAPPING = {
      id column:'o_id'
      version 'ch_id'
      lastUpdated column:'upd'
    }
    
    Map config = (Map)new YamlSlurper().parse( getClass().getResourceAsStream( '/application.yml' ) )
    
    config.gorm[ 'grails.gorm.default.mapping' ] = MAPPING // <- custom mapping injection
    
    new HibernateDatastore( config.gorm, classes as Class[] )
    

    and

    application.yml

    gorm:
      dataSource:
        url: jdbc:h2:file:c:/db/demo-db
        driverClassName: org.h2.Driver
      hibernate:
        hbm2ddl.auto: update
    

    The only gotcha here is, that in none of the domain classes the fields from MAPPING must be present!