Grails域类失败

0 grails constraints grails-domain-class

课程如下:

package tsg

class Country {

    String code
    String codeShort
    String name
    String en
    String nl
    String de
    String fr
    String it
    String es

    static mapping = {
        id name: "code", generator: "assigned"
        version 'revision_number'
    }

    static constraints = {
        code maxSize: 4
        codeShort nullable: true, maxSize: 2
        name nullable: true, maxSize: 100
        en nullable: true, maxSize: 50
        nl nullable: true, maxSize: 50
        de nullable: true, maxSize: 50
        fr nullable: true, maxSize: 50
        it nullable: true, maxSize: 50
        es nullable: true, maxSize: 50
    }
}
Run Code Online (Sandbox Code Playgroud)

当我运行'grails run-app'时,我得到:

| Error 2014-12-12 18:43:55,468 [localhost-startStop-1] ERROR context.GrailsContextLoaderListener  - Error initializing the application: Error creating bean with name 'transactionManagerPostProcessor': Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionManager': Cannot resolve reference to bean 'sessionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory': Invocation of init method failed; nested exception is java.lang.NullPointerException: Cannot invoke method call() on null object
Message: Error creating bean with name 'transactionManagerPostProcessor': Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionManager': Cannot resolve reference to bean 'sessionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory': Invocation of init method failed; nested exception is java.lang.NullPointerException: Cannot invoke method call() on null object
    Line | Method
->>  262 | run       in java.util.concurrent.FutureTask
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|   1145 | runWorker in java.util.concurrent.ThreadPoolExecutor
|    615 | run . . . in java.util.concurrent.ThreadPoolExecutor$Worker
^    745 | run       in java.lang.Thread
Caused by BeanCreationException: Error creating bean with name 'transactionManager': Cannot resolve reference to bean 'sessionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory': Invocation of init method failed; nested exception is java.lang.NullPointerException: Cannot invoke method call() on null object
->>  262 | run       in java.util.concurrent.FutureTask
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|   1145 | runWorker in java.util.concurrent.ThreadPoolExecutor
|    615 | run . . . in java.util.concurrent.ThreadPoolExecutor$Worker
^    745 | run       in java.lang.Thread
Caused by BeanCreationException: Error creating bean with name 'sessionFactory': Invocation of init method failed; nested exception is java.lang.NullPointerException: Cannot invoke method call() on null object
->>  262 | run       in java.util.concurrent.FutureTask
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|   1145 | runWorker in java.util.concurrent.ThreadPoolExecutor
|    615 | run . . . in java.util.concurrent.ThreadPoolExecutor$Worker
^    745 | run       in java.lang.Thread
Caused by NullPointerException: Cannot invoke method call() on null object
->>   25 | doCall    in tsg.Country$__clinit__closure2
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|    262 | run       in java.util.concurrent.FutureTask
|   1145 | runWorker in java.util.concurrent.ThreadPoolExecutor
|    615 | run       in java.util.concurrent.ThreadPoolExecutor$Worker
^    745 | run . . . in java.lang.Thread
| Error Forked Grails VM exited with error
Run Code Online (Sandbox Code Playgroud)
  1. 有趣的是,如果使用语言名称 - en,fr,de等,我会放更长的东西 - lng_en,lng_fr等等.

  2. 如果我取出语言字段的约束,它再次起作用.

  3. 如果我只留下en约束,它就可以了.(其余的 - nl,de,fr,it,es - 被评论).我取消注释nl,运行'grails run-app'并且它有效.我取消注释de,再次运行'grails run-app'并且它有效.但是,如果我一次取消注释2个字段,我会得到相同的错误.可能是什么原因?任何想法如何使其工作(除了重命名字段)?

Jef*_*own 5

你的问题在于变量it.闭包内部it引用闭包的隐式可选参数constraints.你可以通过命名参数来解决这个问题......

class Country {

    String code
    String codeShort
    String name
    String en
    String nl
    String de
    String fr
    String it
    String es

    static mapping = {
        id name: "code", generator: "assigned"
        version 'revision_number'
    }

    static constraints = { arg ->
        code maxSize: 4
        codeShort nullable: true, maxSize: 2
        name nullable: true, maxSize: 100
        en nullable: true, maxSize: 50
        nl nullable: true, maxSize: 50
        de nullable: true, maxSize: 50
        fr nullable: true, maxSize: 50
        it nullable: true, maxSize: 50
        es nullable: true, maxSize: 50
    }
}
Run Code Online (Sandbox Code Playgroud)