如何在Grails中生成数据库模式之前执行脚本?

时间:2016-11-08 17:32:48

标签: grails gorm

我正在使用Grails 2.5.5。在我的bootstrap中,我为String注册了一个静态方法来生成UUID:

String.metaClass.static.uuid = { ->
  java.util.UUID.randomUUID().toString()
}

在我的域类中,我想使用该方法初始化一个uid字段:

class Organization {

   String uid = String.uuid()
   ...
}

问题是当我运行我的Grails应用程序时,它会生成数据库模式,而这似乎在执行Bootstrap之前发生。生成创建域类的实例,但String.uuid()尚未注册,因此我得到一个MissingMethodException。

如何创建一个在数据库模式生成之前运行的脚本,以便在那里注册我的所有方法,以便它们在模式生成时可用?

仅供参考:完整的堆栈跟踪

2016-11-08 17:11:23,638 [localhost-startStop-1] ERROR context.GrailsContextLoaderListener  - Error initializing the application: Error creating b
ean with name 'transactionManagerPostProcessor': Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreatio
nException: Error creating bean with name 'transactionManager': Cannot resolve reference to bean 'sessionFactory' while setting bean property 'se
ssionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory': Invoc
ation of init method failed; nested exception is org.hibernate.MappingException: Could not get constructor for org.codehaus.groovy.grails.orm.hib
ernate.persister.entity.GroovyAwareSingleTableEntityPersister
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 nam
e 'sessionFactory': Invocation of init method failed; nested exception is org.hibernate.MappingException: Could not get constructor for org.codeh
aus.groovy.grails.orm.hibernate.persister.entity.GroovyAwareSingleTableEntityPersister
    Line | Method
->>  262 | run       in java.util.concurrent.FutureTask
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|   1145 | runWorker in java.util.concurrent.ThreadPoolExecutor
|    615 | run . . . in java.util.concurrent.ThreadPoolExecutor$Worker
^    744 | run       in java.lang.Thread
Caused by BeanCreationException: Error creating bean with name 'transactionManager': Cannot resolve reference to bean 'sessionFactory' while sett
ing bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 's
essionFactory': Invocation of init method failed; nested exception is org.hibernate.MappingException: Could not get constructor for org.codehaus.
groovy.grails.orm.hibernate.persister.entity.GroovyAwareSingleTableEntityPersister
->>  262 | run       in java.util.concurrent.FutureTask
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|   1145 | runWorker in java.util.concurrent.ThreadPoolExecutor
|    615 | run . . . in java.util.concurrent.ThreadPoolExecutor$Worker
^    744 | run       in java.lang.Thread
Caused by BeanCreationException: Error creating bean with name 'sessionFactory': Invocation of init method failed; nested exception is org.hibern
ate.MappingException: Could not get constructor for org.codehaus.groovy.grails.orm.hibernate.persister.entity.GroovyAwareSingleTableEntityPersist
er
->>  262 | run       in java.util.concurrent.FutureTask
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|   1145 | runWorker in java.util.concurrent.ThreadPoolExecutor
|    615 | run . . . in java.util.concurrent.ThreadPoolExecutor$Worker
^    744 | run       in java.lang.Thread
Caused by MappingException: Could not get constructor for org.codehaus.groovy.grails.orm.hibernate.persister.entity.GroovyAwareSingleTableEntityP
ersister
->>  262 | run       in java.util.concurrent.FutureTask
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|   1145 | runWorker in java.util.concurrent.ThreadPoolExecutor
|    615 | run . . . in java.util.concurrent.ThreadPoolExecutor$Worker
^    744 | run       in java.lang.Thread
Caused by InstantiationException: could not instantiate test object : com.cabolabs.ehrserver.query.Query
->>  262 | run       in java.util.concurrent.FutureTask
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|   1145 | runWorker in java.util.concurrent.ThreadPoolExecutor
|    615 | run . . . in java.util.concurrent.ThreadPoolExecutor$Worker
^    744 | run       in java.lang.Thread
Caused by InvocationTargetException: null
->>  262 | run       in java.util.concurrent.FutureTask
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|   1145 | runWorker in java.util.concurrent.ThreadPoolExecutor
|    615 | run . . . in java.util.concurrent.ThreadPoolExecutor$Worker
^    744 | run       in java.lang.Thread
Caused by MissingMethodException: No signature of method: static java.lang.String.uuid() is applicable for argument types: () values: []
Possible solutions: wait(), trim(), dump(), find(), wait(long), is(java.lang.Object)
->>   28 | <init>    in com.cabolabs.ehrserver.query.Query
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|    262 | run       in java.util.concurrent.FutureTask
|   1145 | runWorker in java.util.concurrent.ThreadPoolExecutor
|    615 | run       in java.util.concurrent.ThreadPoolExecutor$Worker
^    744 | run . . . in java.lang.Thread

1 个答案:

答案 0 :(得分:0)

我强烈建议您为您的功能创建一个插件,以便您可以使用插件的loadBefore功能来确保在其他插件之前添加您的功能。关于插件的Grails文档详细描述了如何Control Load Order