级联删除不适用于自引用的JPA实体

时间:2015-05-29 08:29:53

标签: java hibernate jpa jpa-2.1

我有类别manu实体:

@Entity
@Table(name = "CATEGORIES")
public class Category {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;
    private String name;

    @OneToMany(cascade = CascadeType.REMOVE)
    @JoinTable(name = "category_subcategory", joinColumns = @JoinColumn(name = "category_id"),
            inverseJoinColumns = @JoinColumn(name = "subcategory_id"))
    private Set<Category> subcategories = new HashSet<>();

当我删除时,我将获得下一个例外:

    SEVERE: Servlet.service() for servlet [dispatcher] in context with path [/] threw exception [Request processing failed; nested exception is org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement] with root cause
org.postgresql.util.PSQLException: error: UPDATE or DELETE in table "categories" violates a foreign key constraint "fk_80ww4t7xfle796ryo5ausl44h"  from table "category_subcategory"
Details: On key (id) = (6) is still referenced in the table "category_subcategory".
at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2102)

请帮助解决这个问题!谢谢!

0 个答案:

没有答案