Generic Top

I just announced the new Learn Spring course, focused on the fundamentals of Spring 5 and Spring Boot 2:

>> CHECK OUT THE COURSE

If you have a few years of experience in the Java ecosystem, and you're interested in sharing that experience with the community (and getting paid for your work of course), have a look at the "Write for Us" page. Cheers. Eugen

1. Overview

In this short tutorial, we’re going to discuss the difference between the save() and saveAndFlush() methods in Spring Data JPA.

Even though both of these methods are used for saving entities to the database, there are some fundamental differences.

2. Example Application

Let’s first see how to use save() and saveAndFlush() methods with an example. As a first step, let’s create an entity class:

@Entity
public class Employee {

    @Id
    private Long id;
    private String name;
    
    // constructors
    // standard getters and setters
}

Next, let’s create a JPA repository for the CRUD operations on the Employee entity class:

public interface EmployeeRepository extends JpaRepository<Employee, Long> {
}

3. The save() Method

As the name depicts, the save() method allows us to save an entity to the DB. It belongs to the CrudRepository interface defined by Spring Data. Let’s see how we can use it:

employeeRepository.save(new Employee(1L, "John"));

Normally, Hibernate holds the persistable state in memory. The process of synchronizing this state to the underlying DB is called flushing.

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.

If we use JPA implementations like Hibernate, then that specific implementation will be managing the flush and commit operations.

One thing we have to keep in mind here is that, if we decide to flush the data by ourselves without committing it, then the changes won’t be visible to the outside transaction unless a commit call is made in this transaction or the isolation level of the outside transaction is READ_UNCOMMITTED.

4. The 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. Here’s how we use it:

employeeRepository.saveAndFlush(new Employee(2L, "Alice"));

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.

5. Conclusion

In this quick article, we focused on the difference between Spring Data JPA save() and saveAndFlush() methods.

In most cases, we’ll be using the save() method. But occasionally, we may need to use the saveAndFlush() method as well for specific use cases.

As usual, the short example we’ve discussed here can be found over on Github.

Generic bottom

I just announced the new Learn Spring course, focused on the fundamentals of Spring 5 and Spring Boot 2:

>> CHECK OUT THE COURSE