I am able to save an entity without @Transactional
in my Spring Boot application. I only noticed this because I wasn't able to update an entity until I added @Transactional
to the save
method within EmployeeService.java
.
I don't have @Transactional
annotated at the class level. The only other place it is found is on methods in my Service layer that do not pertain to the save
method. My EmployeeDAO
does not extend JPARepository
. It is annotated with @Repository
. I have spring-boot-starter-data-jpa
as a dependency. I am not manually beginning or committing a transaction either.
EmployeeRestController.java
@PostMapping("/employees")
public void addEmployee(@RequestBody Employee employee) {
employee.setId(0);
employeeService.save(employee);
}
EmployeeService.java
public void save(Employee employee) {
emplDAO.save(employee);
}
EmployeeDAO.java
@Override
public void save(Employee employee) {
Session currentSession = em.unwrap(Session.class);
currentSession.saveOrUpdate(employee);
}