1. Overview
1.概述
In this quick tutorial, we’ll discuss the difference between the save() and saveAndFlush() methods in Spring Data JPA.
在这个快速教程中,我们将讨论save()和saveAndFlush()方法在Spring Data JPA中的区别。
Even though we use both of these methods for saving entities to the database, there are some fundamental differences.
尽管我们使用这两种方法将实体保存到数据库中,但还是有一些根本的区别。
2. Example Application
2.应用实例
First, let’s see how to use the save() and saveAndFlush() methods with an example. We’ll start by creating an entity class:
首先,让我们通过一个例子看看如何使用save() 和saveAndFlush() 方法。我们将从创建一个实体类开始。
@Entity
public class Employee {
@Id
private Long id;
private String name;
// constructors
// standard getters and setters
}
Next, we’ll create a JPA repository for the CRUD operations on the Employee entity class:
接下来,我们将创建一个JPA资源库,用于对Employee实体类的CRUD操作。
public interface EmployeeRepository extends JpaRepository<Employee, Long> {
}
3. The save() Method
3.save()方法
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:
顾名思义,save()方法允许我们将一个实体保存到DB中。它属于Spring Data定义的CrudRepository接口。让我们来看看我们如何使用它。
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.
通常情况下,Hibernate在内存中保存可持续状态。将这个状态同步到底层数据库的过程被称为刷新。
When we use the save() method, the data associated with the save operation won’t be flushed to the DB unless, and until, an explicit call to the flush() or commit() method is made.
当我们使用save()方法时,与保存操作相关的数据不会被刷入DB,除非并且直到对flush()或commit()方法的明确调用。
If we use JPA implementations like Hibernate, then that specific implementation will be managing the flush and commit operations.
如果我们使用像Hibernate这样的JPA实现,那么该具体实现将管理刷新和提交操作。
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.
在这里我们要记住的一点是,如果我们决定自己刷新数据而不提交,那么外部事务将无法看到这些变化,除非在这个事务中进行了提交调用,或者外部事务的隔离级别为READ_UNCOMMITTED。
4. The saveAndFlush() Method
4、saveAndFlush()方法
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:
与save()不同,saveAndFlush()方法在执行过程中立即刷新了数据。该方法属于Spring Data JPA的JpaRepository接口。下面是我们如何使用它的。
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 that we’re going to save. In this case, the save() method won’t work, since the changes aren’t 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.
例如,想象一下这样的情景:我们必须执行一个存储过程,该存储过程期望我们要保存的实体的一个属性。在这种情况下,save()方法将不起作用,因为变化没有与DB同步,而且存储过程不知道这些变化。saveAndFlush()方法完全适用于这种情况。
5. Conclusion
5.结论
In this brief article, we focused on the difference between the Spring Data JPA save() and saveAndFlush() methods.
在这篇简短的文章中,我们重点讨论了Spring Data JPA save()和saveAndFlush()方法之间的区别。
In most cases, we’ll use the save() method. But occasionally, we may need to use the saveAndFlush() method as well for specific use cases.
在大多数情况下,我们会使用save()方法。但偶尔,我们可能需要在特定的使用情况下使用saveAndFlush()方法。
As usual, the example we’ve discussed here can be found over on GitHub.
像往常一样,我们在这里讨论的例子可以在GitHub上找到超过。