Difference between save and saveAndFlush in Spring data jpa

hibernate, java, jpa, spring, spring-data-jpa

Solution

On `saveAndFlush`, changes will be flushed to DB immediately in this command. With `save`, this is not necessarily true, and might stay just in memory, until `flush` or `commit` commands are issued.

But be aware, that even if you flush the changes in transaction and do not commit them, the changes still won't be visible to the outside transactions until the commit in this transaction.

In your case, you probably use some sort of transactions mechanism, which issues `commit` command for you if everything works out fine.

Problem

I am trying to learn spring data JPA by testing some CRUD operation via `JpaRepository`. I came across two methods `save` and `saveAndFlush`. I don't get the difference between these two. On calling `save` also my changes are getting saved into database so what is the use of `saveAndFlush`.

Original source

Related problems