Difference between save and saveAndFlush in Spring data jpa

JavaSpringHibernateJpaSpring Data-Jpa

Java Problem Overview


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.

Java Solutions


Solution 1 - Java

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.

Solution 2 - Java

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.

Solution 3 - Java

In Spring Data Jpa save() method allows us to save an entity to the DB. It belongs to the CrudRepository interface defined by Spring Data.

When we use the save() method, the data associated with the save operation will not be flushed to the DB unless and until an explicit call to flush() or commit() method is made.

As an example, let's create an Entity and JPA repository.

@Data
@Entity
public class User{

    @Id
    private Long id;
    private String name;
}


public interface UserRepository extends JpaRepository<User, Long> {
}

Then can be used save() method like this,

userRepository.save(new User(1L, "Geeth"));

But saveAndFlush() method unlike save(). The saveAndFlush() method flushes the data immediately during the execution. This method belongs to the JpaRepository interface of Spring Data JPA. you can use it as follows.

userRepository.saveAndFlush(new User(2L, "Sam"));

Normally, we use this method when our business logic needs to read the saved changes at a later point during the same transaction but before the commit.

For instance, imagine a scenario where we have to execute a stored procedure that expects a property of the entity, which we're going to save. In this case, the save() method won't work since the changes are not in sync with the DB and the stored procedure doesn't know about the changes. The saveAndFlush() method is perfectly suited for this kind of scenario.

Solution 4 - Java

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAnandView Question on Stackoverflow
Solution 1 - Javauser1918305View Answer on Stackoverflow
Solution 2 - JavaRalfView Answer on Stackoverflow
Solution 3 - JavaGeethView Answer on Stackoverflow
Solution 4 - JavaDinushika RathnayakeView Answer on Stackoverflow