I am a beginner in spring boot and there is one thing that confuses me. As I understand it, in spring data jpa an EntityManger of type "container-managed transaction-scoped persistence context" is used. So according to my assumption, for each transactional service method a separate EntityManager should always be created to manage the entities within the method and after the method terminates all managed entities should be in detached state.
In the example below, StudentController calls 2 StudentService transactional methods. I thought that "studentService.insertStudent(studentEntity)" returns a detached entity and therefore "studentService.updateStudent(studentEntity1)" has no effect. But on the contrary, the entity is updated in DB, which means that it is still managed.
StudentController.java
@RequestMapping(
value = "/student",
method = {RequestMethod.PUT},
consumes = "application/json",
produces = "application/json"
)
public ResponseEntity<StudentEntity> insertStudent(@RequestBody StudentEntity studentEntity) {
StudentEntity studentEntity1 = studentService.insertStudent(studentEntity);
StudentEntity studentEntity2 = studentService.updateStudent(studentEntity1);
return new ResponseEntity<>(studentEntity2, HttpStatus.OK);
}
StudentService.java
@Transactional
public StudentEntity insertStudent(StudentEntity studentEntity){
return studentRepository.save(studentEntity);
}
@Transactional
public StudentEntity updateStudent(StudentEntity studentEntity){
studentEntity.setFirstName("firstName!!");
return studentEntity;
}
can someone please help me?