Difference between Save and SaveAndFlush in Spring Java


Save and saveAndFlush both can be used for saving entities. They both are both belong to the Spring data library. save may or may not write your changes to the DB straight away. When we call saveAndFlush system are enforcing the synchronization of your model state with the DB.

Sr. No.KeySaveSaveAndFlush
1
Repository
It belongs to CrudRepository
It belongs to JPARepository
2
Data flush Strategy
It doesn't flush data directly to a database until and unless we explicitly call flush and commit method.
It's flush directly flush data to a database.
3
Bulk Save
CrudRepository provides bulk save method
saveAndFlush method doesn't support the bulk operation
 4
Data Visibility after saving
It doesn't flush data directly to a database, therefore, changes will not be visible outside the transaction unless we explicitly call commit() in this transaction.
Changes will be visible outside the transaction also.
5
Use Case
We use this method when we don't need to use the saved changes at a later point in the same transaction.
We use this method when we need to use the saved changes at a later point in the same transaction.

Updated on: 18-Nov-2019

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements