Why when I use spring boot save method it changes the persistence of the data in several places?

Asked

Viewed 19 times

-1

Talk personal, all right? In my context, I need to do an undo method, this out using an array object that saves in memory my last given database persintência and, when saved this data in the array and call the save method (of JPA’s Springboot), with the new data changed in the database, this save method changes the persistence within the array.

public ResponseEntity putLoja(@Valid @RequestBody Loja atualizaLoja, @PathVariable int idLoja) {
        if (lojaRepository.existsById(idLoja)) {
            atualizaLoja.setId(idLoja);
            lojaRepository.save(atualizaLoja);
            pilhaLojaAuxiliar.push(lojaRepository.findById(idLoja).get());
            return ResponseEntity.status(200).build();
        } else {
            return ResponseEntity.status(400).build();
        }
    }

1 answer

1

Hello, in your case the simplest solution is to perform the de-structuring of the created object a new, so keep the accuracy since the data of your arrey is no longer linked to the data being saved by save method()

public ResponseEntity putLoja(@Valid @RequestBody Loja atualizaLoja, @PathVariable int idLoja) {
    if (lojaRepository.existsById(idLoja)) {
        atualizaLoja.setId(idLoja);
        lojaRepository.save(atualizaLoja);
        //Solução aqui
        Loja lojaDataPast = lojaRepository.findById(idLoja).get()
        Loja lojaDataArray = new Loja();
        lojaDataArray.setId(idLoja);
        lojaDataArray.setNome(lojaDataPast.getNome())
        pilhaLojaAuxiliar.push(lojaDataArray);
        //Fim Solução
        return ResponseEntity.status(200).build();
    } else {
        return ResponseEntity.status(400).build();
    }
}

Browser other questions tagged

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