Spring data jpa @ManyToOne orphanRemoval=true 不删除

Jev*_*man 6 java mysql orm spring-data-jpa

情况:有书籍和类别。每个类别可以有 n 本书。想要从类别中创建新书、更新书或删除书。如果类别为空,则从数据库中删除它。当前插入、更新和删除书籍。

问题: orphanRemoval=true 如果类别为空,则不会删除类别。

@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> {
}
Run Code Online (Sandbox Code Playgroud)

谢谢你的帮助

ps
尝试过:
没有帮助:
带有 orphanRemoval 的 Spring + JPA @OneToMany
看起来很愚蠢 - 从类别中删除书籍(如果 2-n 外键不起作用):
https : //hellokoding.com/jpa-one-to-many-relationship- mapping-example-with-spring-boot-maven-and-mysql/
没有提到: https :
//docs.spring.io/spring-data/jpa/docs/current/reference/html/