课程如下:
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
有趣的是,如果使用语言名称 - en,fr,de等,我会把更长的东西 - lng_en,lng_fr等等用。
如果我取出语言字段的约束,它又有效。
如果我只留下en约束,它就可以了。(其余的 - nl,de,fr,it,es - 被评论)。我取消注释nl,运行' grails run-app'它的工作原理。我取消注释,运行' grails run-app'再次,它的工作原理。但是,如果我一次取消注释2个字段,我会得到相同的错误。 可能是什么原因?任何想法如何使其工作(除了重命名字段)?
答案 0 :(得分: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
}
}