【发布时间】:2017-09-17 01:39:39
【问题描述】:
下面是 Spring Data JPA 中 save() 方法的 Javadoc
/**
* Saves a given entity. Use the returned instance for further operations as the save operation might have changed the
* entity instance completely.
*
* @param entity
* @return the saved entity
*/
<S extends T> S save(S entity);
下面的资源也明确指出我们需要使用返回的对象来确保我们对保存的实体执行进一步的操作。
Why to use returned instance after save() on Spring Data JPA Repository?
但是,在我下面的代码中,我的 save() 实体可以正常工作,而无需
public void createEmployee(Employee employee) throws Exception {
try {
employee.setEmployeeName("alan");
employeeRepo.save(employee);
employee.setEmployeeType("Permanent");
employeeRepo.save(employee);
} catch (Exception e) {
throw new Exception(e);
}
}
- 为什么上面的代码在没有返回类型的情况下工作
- 测试似乎表明数据的持久性非常好,但我的印象是我们需要使用返回的实体来确保正确的持久性。
我是否在无意中做对/错的事情?我是 spring-data 的新手,想完全理解 save() 方法,因为我当前的很多应用程序都是基于 CRUD 的。谢谢。
【问题讨论】:
标签: java hibernate jpa spring-data spring-data-jpa