【问题标题】:Grails domain class failingGrails 域类失败
【发布时间】:2014-12-12 17:31:28
【问题描述】:

类如下:

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

当我运行“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
  1. 有趣的是,如果取而代之的是语言名称 - en、fr、de 等。我输入更长的名称 - lng_en、lng_fr 等。它可以工作。

  2. 如果我去掉语言字段的约束,它会再次起作用。

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

【问题讨论】:

    标签: grails constraints grails-domain-class


    【解决方案1】:

    您的问题在于变量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
        }
    }
    

    【讨论】:

    • 你也可以让闭包根本不接受参数,比如static constraints = { ->
    • 谢谢。我为此苦苦挣扎了2天。你是最棒的
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-12
    相关资源
    最近更新 更多