Search code examples
hibernategrailsgrails-orm

Any documentation for `saveAll`?


I have come across some Grails code as follows:

Book.saveAll(bookList)

where bookList is a List of Book domain instances. While this appears to be working fine (saving all the Book instances) while run-app, functional tests fail with the following error:

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.

Then I wanted to see some documentation about saveAll and I cannot seem to be able to find any. Is there any documentation? Should this method even be used?


Solution

  • I'm not sure why that's not documented, but there are quite a few gaps in the docs :) The implementation is pretty basic though, it just loops through the list and calls save on each. So you could convert it to your own loop and it should work in both scenarios, e.g.

    for (book in bookList) {
       book.save()
    }
    

    This has the advantage that you can check the errors of each in the loop, and it's more obvious that multiple things are being saved and that this should be done in a transaction.