Я использую Grails 2.5.5. В моем загрузчике я зарегистрировал статический метод Строки для генерации UUID,:Как выполнить скрипт перед генерацией схемы базы данных в Grails?
String.metaClass.static.uuid = { ->
java.util.UUID.randomUUID().toString()
}
И на моих классах предметной области, я хочу использовать этот метод для инициализации Uid поля:
class Organization {
String uid = String.uuid()
...
}
Проблема заключается в том, когда Я запускаю приложение Grails, он генерирует схему базы данных, и это, похоже, происходит до выполнения Bootstrap. Генерация создает экземпляры классов домена, но String.uuid() еще не зарегистрирован, поэтому я получаю исключение MissingMethodException.
Как создать скрипт, который выполняется до генерации схемы базы данных, чтобы зарегистрировать все мои методы там, чтобы они были доступны при создании схемы?
FYI: полный StackTrace
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
В моем случае это было бы что-то вроде loadBefore = ['domain']? Документы не отображают все возможные значения для loadBefore. –
На самом деле, я думаю, вы хотите 'loadBefore = ['database-migration']', поэтому ваш плагин загружается до плагина миграции базы данных. Я не уверен на 100% имени плагина, но быстрый взгляд на источник заставляет меня думать, что это было правильно. –