I'm using spring-boot 1.5.4 with hibernate-core 5.2.10.
I have a controller that invokes a service method (lets name it pService) to save an entity after some logic
pService.save is something like this:
@Transactional(readOnly = false, rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
public <S extends SomeEntity> S save(S entity) throws ServiceException {
//some logic
entity.setAttrX("original text");
S justSavedEntity = someEntityRepository.save(entity); // POINT 1
//we termporarily change one attribute just to get a legible representation calling another service method (lets name it eService)
eService.process(justSavedEntity);
return justSavedEntity; //POINT 2
}
eService.process is something like this:
@Transactional(readOnly = false, propagation = Propagation.MANDATORY)
public void process(SomeEntity entity) {
//some logic
entity.setAttrX("someText");
//method ends here
}
Because there's no other call to save apart of the one at POINT 1, I expect the text "original text" to be saved in database, but the text I'm getting saved is the text changed at POINT 2.
So, why I'm getting saved "someText" instead of "original text"?
I can see in the database log that after POINT 1 an SQL INSERT command is runned and when pService returns, and unwanted SQL UPDATE command is runned changing "original text" to "someText" causing the error.
why is this "extra" UPDATE command issued?
Thanks in advance!