Why does spring data persist when I use a set?

Asked

Viewed 57 times

1

I have a method more or less like the following within a service:

@Transactional
public void atualizarPosicoes(Integer novaPosicao, Long idTarefaAtualizada){
    this.repositorio.listAllTarefas().forEach((Tarefa tarefa) -> {
        if(tarefa.getId().equals(idTarefaAtualizada)){
            tarefa.setPosicao(novaPosicao);
        }
    });
}

See that in no time I execute one this.repositorio.save(tarefa) but at the end of the execution of my method my task in DB is updated. I tried to use the .stream() but the result is the same, in my specific case this is the expected behavior, but I was left with the doubt, because it persists even without me to execute the .save()?

  • Interesting, but are you sure you have not manipulated the object before? Did you update it before this method?

  • Yes, the method does just that, search for the repository and a set, but Lucas Miranda’s answer clarified.

1 answer

2


I believe it’s because of your note @Transactional, it opens the transaction at the beginning and if all goes well it performs the commit at the end, it has a very interesting explanation about this annotation here:

Operation of the Spring Framework @Transactional

Browser other questions tagged

You are not signed in. Login or sign up in order to post.