English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

The Difference Between Save and SaveAndFlush in Spring Java

Both Save and saveAndFlush can be used to save entities. They both belong to the Spring database. Saving may or may not immediately write your changes to the database. When we call saveAndFlush, the system will force the model state to synchronize with the database.

Serial numberKeyRescueSave and refresh
1
Repository
It belongs to CrudRepository
It belongs to JPARepository
2
Data refresh strategy
Unless and unless we explicitly call the flush and commit methods, it will not refresh the data directly to the database.
Refresh will directly refresh the data to the database.
3
Batch save
CrudRepository provides batch save methods
The saveAndFlush method does not support batch operations
 4
Visibility of saved data
It will not refresh the data directly to the database, so, unless we explicitly callcommit()This transaction, otherwise changes will not be visible outside the transaction.
Changes will also be visible outside the transaction.
5
Use case
When we no longer need to use saved changes in the same transaction, we can use this method.
You can use this method when you need to use the saved changes in the same transaction later.