使用 Grails/GORM 清除和恢复夹具数据

Clear out and restore fixture data using Grails/GORM

我需要根据控制器请求重置 GORM 夹具数据 (H2)。所以我创建了以下控制器...

def dataSource
FixtureLoader fixtureLoader
def index() {
    Sql sql = new Sql(dataSource)
    sql.execute("DROP ALL OBJECTS DELETE FILES")
    sql.close()
    fixtureLoader.load('f1','f2','f3')
}

我希望这可以清除数据库,但是,我仍然收到以下错误

[org.codehaus.groovy.grails.web.errors.GrailsExceptionResolver] NonUniqueObjectException occurred when processing request: [GET] a different object with the same identifier value was already associated with the session: [*]. Stacktrace follows: org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: [*]

另一个奇怪的事情是,如果我进入 dbconsole 并使用 'DROP ALL OBJECTS DELETE FILES' 命令清除所有内容,当我进入控制器时,我会得到同样的错误。尽管我可以确认它们已从数据库中删除。

更新

所以我尝试了一些东西...

1.) 'DROP ALL OBJECTS DELETE FILES' 似乎在 dbconsole 中按预期工作。但是代码

    Sql sql = new Sql(dataSource)
    sql.execute("DROP ALL OBJECTS DELETE FILES")
    sql.close()

好像效果不一样

2.) 我添加了以下几行

def sessionFactory
...
sql.close()
sessionFactory.currentSession.flush() //This one  
fixtureLoader.load('f1','f2','f3')

然后我去数据库控制台删除(因为 SQL 似乎失败了),然后 运行。然而,当我再次 运行 函数时,我仍然看到了固定装置。

为了做到这一点,我基本上和 accepted answer here 一样...

在我的 Bootstrap 中,我创建了 tmp 文件,然后当控制器被调用时,我 运行 它作为脚本,一切都被重置。我还必须确保根据评论删除了缓存。