Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

263
Visualizações
Spring JPA ManyToOne deletion handle

I have two POJOs, one is for Post which is below

@Entity
public class Post {
    ...
    @ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    @JoinColumn(name = "category_id")
    private Category category;
    ...
}

and another one is for Category as below

@Entity(name = "category")
public class Category {
    ...
    @OneToMany(fetch = FetchType.LAZY, mappedBy = "category", cascade = CascadeType.ALL)
    @JsonIgnore
    private List<Post> post;
    ...
}

I have two crud repositories for each class. And here is my issue. GET, POST, PUT is working as expected. But for DELETE it is creating the issue. Suppose POST A has Category type C. When I am trying to delete POST A it is not only deleting POST A itself also deleting Category C and all Posts related to Category C.

Please help :)

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

The problem is in the CascadeType, of the Post side. CascadeType.ALL includes CascadeType.REMOVE, so when you delete a post, the category to which that post belongs will be removed as well. Retain only CascadeType.ALL on the Category side and remove it from Post.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda