1

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?

S_M
  • 11
  • 3
  • First thoughts: After save you are not returning the saved entity. You should use the return value of the save operation. Further the entity is automatically updated on your `updateStudent` method as you don't need to explicitly call `save`in `@Transactional` methods. With updated you mean it is updated in the DB? – Daniel Rafael Wosch Jun 02 '22 at 11:56
  • yes it has been updated in the DB. And I saw that updateStudent(StudentEntity studentEntity) sent an update query to DB. How could this happen? Is studentEntity not in detached state during updateStudent method? – S_M Jun 02 '22 at 12:08
  • 1
    The behaviour you are facing should be related to the Spring Open Session in View "pattern". For every incoming request a db sessions gets created no matter if a db session is necessary or not. Therefore your call to the endpoint runs in the same session and the entity does not get detached. See: https://stackoverflow.com/a/48222934/11079633 Try setting `spring.jpa.open-in-view=false` and see if it helps – Daniel Rafael Wosch Jun 02 '22 at 12:12
  • it seems to work, thank you very much. Should this option always be deactivated? – S_M Jun 02 '22 at 13:22
  • You are welcome. Like always: It depends! Read through the provided stack overflow post to get a little insight about it and decide by your own. – Daniel Rafael Wosch Jun 02 '22 at 15:52

0 Answers0