db迁移插件中的grailsChange问​​题

时间:2015-10-08 17:15:44

标签: hibernate grails database-migration grails-3.0

我正在使用Grails 3.0.8的database-migration:2.0.0.RC1。以下是更改集失败的示例:

changeSet(author: '...', id: '...') {
    grailsChange {
        change {
            final BATCH_SIZE = 500
            long offset = 0

            def loadBatchVers1 = { Class clazz ->
                clazz.findAll("from ${clazz.simpleName}", [], [max: BATCH_SIZE, offset: offset])
            }


            SessionFactory sessionFactory = ctx.getBean("sessionFactory")
            def loadBatchVers2 = { Class clazz ->
                Criteria criteria = sessionFactory.currentSession.createCriteria(clazz)
                criteria.setFirstResult(offset)
                criteria.setMaxResults(BATCH_SIZE)
                return criteria.list()
            }

            // Load domain objects using loadBatchVers1 or loadBatchVers2 and make changes in db
         }
     }
}

当我使用loadBatchVers1加载域对象时,这是我收到的例外:

 Method on class [XXXX] was used outside of a Grails application. If running in the context of a test using the mocking API or bootstrap Grails correctly.

我认为这可能是因为我正在使用域类的静态方法findAll,所以我实现了版本loadBatchVers2loadBatchVers2的例外情况:

Error: No Session found for current thread

我无法使用静态域方法或hibernate条件api在 changeSet grailsChange 部分中进行任何更改。

0 个答案:

没有答案