Spring: Delete data from tables with relationships

Viewed 51

I developed a CRUD in spring using thymeleaf. Everything is working. Some tables have OneToOne and ManyToOne relationships. And every time I delete a row from the news table for example, it also deletes from the author table. How do I prevent this?

NewsServiceImplement:

@Override
public void delete(Long id) {
    repository.deleteById(id);
}

DashNewsController:

// Delete
@RequestMapping("/dashboard/noticias/deletar/{id}")
public String destroy(@PathVariable("id") Long id) {
    noticiaService.delete(id);
    return "redirect:/dashboard/noticias";
}
1 Answers

Make sure you don't have the cascade attribute where you defined the relationship (@OneToMany), if you do just remove it:

cascade = CascadeType.ALL
Related