In my previous blog we have seen how to set failOnError and flush globally. In this blog I am providing a use case of failOnError and flush.

Lets have a simple domain class

  • failOnError: when it is set to true then save() method will throw ValidationException when validation fails during save.

In the above code snippet, I have given invalid email address in both the places but in second try..catch I have used failOnError while saving the Person instance. But in domain class we have declared email: true so application does not allow us to save the data. And should warn us for that. Lets see what we got in console

While first save (without failOnError: true) our catch block is not executed and person instance is not saved as well, person count is still 1. But in second save (with failOnError: true) catch block is executed and there we can warn user that data was not saved.



  • flush: when it is set to true then save() method flush the session immediately and save data in the database.

In the above code snippet, at first I am creating a person with email address manish@example.com then deleting and creating another person with the same email address. If we run the code we got the following exception

This is because we are trying to create a new person with the same email address while another person still exists with the given email address.

To overcome this exception we need to use flush: true, like:

And our method execute correctly.

Console output:

Hope this helps. 🙂