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

372
Visualizações
Spring data jpa @ManyToOne orphanRemoval=true doesn't delete

Situation: Got Books and categories. Each category can have n books. Want to create new, update or delete books from category. If category is empty delete it from db. Currently insert, update, and delete books.

Problem: orphanRemoval=true doesn't delete category if it is empty.

@Entity
@Table(name = "book")
@Getter
@Setter
public class BookEntity implements DBEntity, Serializable {

    @Id
    private Integer id;
    private String bookname;
    @ManyToOne(cascade = {CascadeType.MERGE, CascadeType.PERSIST, CascadeType.DETACH, CascadeType.REFRESH})
    @JoinColumn(name = "categoryid", referencedColumnName = "id")
    private CategoryEntity categoryEntity;

    public Integer getId() {
        return this.id;
    }
}

@Entity
@Table(name = "category")
@Getter
@Setter

public class CategoryEntity implements DBEntity, Serializable {
    @Id
    private Integer id;
    private String categoryName;

    @OneToMany(cascade = CascadeType.REMOVE, mappedBy = "categoryEntity", orphanRemoval = true)
    private Set<BookEntity> bookEntitySet = new HashSet<BookEntity>(0);

}

@Transactional
public interface CategoryDAO extends CrudRepository<CategoryEntity, Integer> {
}

@Transactional
public interface BookDAO extends CrudRepository<BookEntity, Integer> {
}

Thanks for any help

p.s.
Tried:
didnt help:
Spring + JPA @OneToMany with orphanRemoval
looks stupid - deleting book from category (in case 2-n foreign key wont work):
https://hellokoding.com/jpa-one-to-many-relationship-mapping-example-with-spring-boot-maven-and-mysql/
Nothing mentioned:
https://docs.spring.io/spring-data/jpa/docs/current/reference/html/

over 4 years ago · Santiago Trujillo
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