175

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.

4 Answers 4

190

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.

4
  • 58
    "won't be visible to the outside transactions until the commit in this transaction" It depends on the isolation level of the other transactions. If a transaction's isolation level is READ_UNCOMMITTED,then it will see what has been flushed but not yet committed by other transactions. Commented Jan 31, 2017 at 9:46
  • 1
    but, in my project, I use save(), saveAll() & it persists in DB without commit or flush calling explicitly. Then why should I prefer saveAndFlush? FLush mode all those things are in default mode Commented Nov 6, 2019 at 3:19
  • 1
    P Satish Patro, saveAndFlush() is used for immediate flush. If you use save(), the flush action will be handled by JPA later.
    – apr
    Commented Aug 19, 2020 at 7:37
  • @Gab是好人 please note that, for example, Postres does not support READ_UNCOMMITTED and uses READ_COMMITTED instead. It may save a lot of time for you. The SQL standard defines one additional level, READ UNCOMMITTED. In PostgreSQL READ UNCOMMITTED is treated as READ COMMITTED.
    – demitt
    Commented May 17 at 22:20
56

Depending on the hibernate flush mode that you are using (AUTO is the default) save may or may not write your changes to the DB straight away. When you call saveAndFlush you are enforcing the synchronization of your model state with the DB.

If you use flush mode AUTO and you are using your application to first save and then select the data again, you will not see a difference in bahvior between save() and saveAndFlush() because the select triggers a flush first. See the documention.

2
  • sorry but if I save an entity and again the same one, you mean with save the second command won't throw an exception for duplication for example??
    – azerafati
    Commented Jul 5, 2014 at 12:23
  • 3
    @Bludream Yes, it is my understanding that calls to save are idempotent. See this thread for more details.
    – Ralf
    Commented Jul 6, 2014 at 7:30
7

Both methods are used to save entities to the database. Flushing is the process of synchronizing the state of the persistence context with the underlying database.

When using saveAndFlush method, data immediately flush to the database and to do it with the save method we need to call flush() method explicitly. Using flush can read saved changes at a later step during the same transaction but before the commit. So still can rollback if no need to commit.

4

The "save" method is used to save an entity to the database. When you call the "save" method, the entity is first persisted to the database's transactional buffer, and then, when the transaction is committed, the entity is saved to the database. The "save" method returns the saved entity.

The "save and flush" method, on the other hand, does the same thing as "save", but additionally forces the database to immediately write the pending changes to disk. This can be useful in cases where you need to ensure that the data is immediately persisted to the database, rather than waiting for the transaction to commit.

In summary, the main difference between "save" and "save and flush" in Spring Data is that the latter immediately writes the pending changes to the database, while the former waits until the transaction is committed to do so.

If you want to save a new entity and use its ID within the same transaction, you should use the "save" method followed by a call to the "flush" method.

When you call the "save" method, the entity is persisted to the transactional buffer, and a temporary ID is assigned to the entity. This ID is generated by the underlying persistence framework and is not the final ID that will be assigned to the entity when it is saved to the database.

By calling the "flush" method, you force the underlying persistence framework to immediately write the pending changes to the database. This causes the temporary ID assigned to the entity to be replaced with the final ID assigned by the database.

Once the "flush" method has been called, you can safely use the ID of the newly saved entity within the same transaction.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.