micronaut-groovy icon indicating copy to clipboard operation
micronaut-groovy copied to clipboard

Cannot define default mapping/constraints

Open jamesdh opened this issue 4 years ago • 3 comments

Task List

  • [x] Steps to reproduce provided
  • [ ] Stacktrace (if present) provided
  • [ ] Example that reproduces the problem uploaded to Github
  • [x] Full description of the issue provided (see below)

Steps to Reproduce

  1. Create a fresh app w/ groovy/GORM
  2. Create a generic GORM domain class
  3. Create a src/main/resources/application.groovy file w/ the following added:
import static grails.gorm.hibernate.mapping.MappingBuilder.orm

grails.gorm.default.mapping = orm {
    id {
        generator('org.hibernate.id.enhanced.SequenceStyleGenerator')
        params([prefer_sequence_per_entity: true])
    }
}

// or try it with a more traditional style...
//grails.gorm.default.mapping = orm {
//    id([
//            generator: 'org.hibernate.id.enhanced.SequenceStyleGenerator',
//            params:[prefer_sequence_per_entity: true]
//    ])
//}

//grails.gorm.default.mapping = {
//    id {
//        generator = 'org.hibernate.id.enhanced.SequenceStyleGenerator'
//        params = [prefer_sequence_per_entity: true]
//    }
//}

Expected Behaviour

The default mapping would be applied to the domain, and a sequence would be generated based on the domain classes name.

Actual Behaviour

When defining the mapping via the MappingBuilder DSL, it does not appear to ever be executed, and when defining the mapping using the traditional closure, it fails due to static type checking, e.g.

Script1.groovy: 19: [Static type checking] - Cannot find matching method Script1#id(groovy.lang.Closure). Please check if the declared type is correct and if the method exists.

Environment Information

  • Operating System: MacOS 10.15.7
  • Micronaut Version: 2.3.1
  • JDK Version: 11

jamesdh avatar Feb 04 '21 23:02 jamesdh

Try setting the system property micronaut.groovy.config.compileStatic to false

jameskleeh avatar Feb 11 '21 21:02 jameskleeh

Seems that system property isn't documented. Please verify that it works and if so, we can leave this issue open to document it

jameskleeh avatar Feb 11 '21 21:02 jameskleeh

Can confirm, disabling compileStatic on configs via gradle e.g.

tasks {
    named<JavaExec>("run") {
        systemProperty("micronaut.groovy.config.compileStatic", "false")
    }
}

...and then using the closure form of grails.gorm.default.mapping does work e.g.

grails.gorm.default.mapping = {
     id generator: 'org.hibernate.id.enhanced.SequenceStyleGenerator', params: [prefer_sequence_per_entity: true]
}

Using the MappingBuilder.orm static method does not:

grails.gorm.default.mapping = orm {
    id {
        generator('org.hibernate.id.enhanced.SequenceStyleGenerator')
        params([prefer_sequence_per_entity: true])
    }
}

jamesdh avatar Feb 11 '21 22:02 jamesdh