Below is the Javadoc specific for the save() method in Spring Data JPA
/**
* 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);
and also below resource clearly states that we need to use the returned objected to ensure that we perform further operations on the saved entity.
Why to use returned instance after save() on Spring Data JPA Repository?
However, in my below code my save() entity works fine without
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);
}
}
- Why is the above code working without a return type
- Tests seem to show that data is being persisted perfectly fine, but i was under the impression that we need to use the returned entity to ensure correct persistence.
Am I doing something inadvertently right/wrong ? I am new to spring-data and would like to understand save() method perfectly since a lot of my current application is CRUD based. Thanks.