zor*_*119 2 grails hibernate grails-orm
我遇到了一些Grails代码如下:
Book.saveAll(bookList)
Run Code Online (Sandbox Code Playgroud)
其中bookList是一个List的Book域实例。虽然这似乎工作正常(保存所有Book实例) while run-app,但功能测试失败并出现以下错误:
Write operations are not allowed in read-only mode (FlushMode.MANUAL): Turn your Session into FlushMode.COMMIT/AUTO or remove 'readOnly' marker from transaction definition.
Run Code Online (Sandbox Code Playgroud)
然后我想查看一些有关的文档saveAll,但似乎找不到任何文档。有什么文件吗?甚至应该使用这种方法吗?
基本上, saveAll 不会刷新。使用 saveAll 后,如果您想立即在数据库中进行更改,您可以获取当前会话并刷新它。这就是 Hibernate 的工作原理:当您调用 flush 时,它将刷新 Hibernate 会话,立即将会话中的所有数据持久化到数据库中。如果不使用 (flush:true),则数据仅记录在 Hibernate 会话中,并且只有在刷新 Hibernate 会话时才会持久保存在数据库中。刷新会话的时间由 Hibernate 自动确定以优化性能。有不同的刷新方式,您可以保存所有数据并最终刷新。
def hibSession = sessionFactory.getCurrentSession()
hibSession.flush()
Run Code Online (Sandbox Code Playgroud)
我们遇到了同样的问题。测试哪种方法更快。测试用 300 个项目完成。
list.each{ Book.save(flush: true, failOnError: true) }
list.each{Book.save()};
def hibSession = sessionFactory.getCurrentSession();
hibSession.flush()
Book.saveAll(list);
def hibSession = sessionFactory.getCurrentSession();
hibSession.flush()
| 归档时间: |
|
| 查看次数: |
4034 次 |
| 最近记录: |