Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

371
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda