Hibernate:由于外键违规而不允许级联删除

时间:2015-05-11 12:34:36

标签: java spring hibernate postgresql spring-mvc

我正在开发一个Spring-MVC应用程序,我试图删除一个实体,该实体与其他3个实体有一对多的关系。我在Cascade上标记了要删除的实体,但它无法正常工作。我收到外键违规错误..

Notes实体:

@Entity
@Table(name="groupnotes")
public class GroupNotes {

    @OneToMany(mappedBy = "mnotedata",fetch = FetchType.EAGER,cascade = CascadeType.REMOVE)
    private Set<GroupAttachments> mattachments = new HashSet<>();

    public Set<GroupAttachments> getMattachments() {
        return this.mattachments;
    }

    public void setMattachments(Set<GroupAttachments> mattachments) {

        this.mattachments = mattachments;
    }

    @OneToMany(mappedBy = "mhistory",fetch = FetchType.LAZY,cascade = CascadeType.REMOVE)
    private Set<GroupNoteHistory> groupNoteHistorySet = new HashSet<>();

    public Set<GroupNoteHistory> getGroupNoteHistorySet(){
        return this.groupNoteHistorySet;
    }

    public void setGroupNoteHistorySet(Set<GroupNoteHistory> groupNoteHistorySet){
        this.groupNoteHistorySet = groupNoteHistorySet;
    }

    @OneToMany(mappedBy = "unreadNotes",fetch = FetchType.LAZY,cascade = CascadeType.REMOVE)
    private Set<UnreadNotes> unreadNotesSet = new HashSet<>();

    public Set<UnreadNotes> getUnreadNotesSet(){
        return this.unreadNotesSet;
    }

    public void setUnreadNotesSet(Set<UnreadNotes> unreadNotesSet){
        this.unreadNotesSet = unreadNotesSet;
    }
}

实体组附件:

@Entity
@Table(name = "groupattachments")
public class GroupAttachments {
@ManyToOne
    @JoinColumn(name = "mnoteid")
    @OnDelete(action = org.hibernate.annotations.OnDeleteAction.CASCADE)
    private GroupNotes mnotedata;

    public void setMnotedata(GroupNotes mnotedata){this.mnotedata=mnotedata;}

    public GroupNotes getMnotedata(){return mnotedata;}
}

但是当我执行下面的代码时:

 @Override
    public boolean deleteAllNotesForSection(int msectionid) {
        session = this.sessionFactory.getCurrentSession();
        org.hibernate.Query query = session.createQuery("from GroupNotes as " +
                "n where n.ownednotes.msectionid=:msectionid");
        query.setParameter("msectionid",msectionid);
        List<GroupNotes> groupNotesList = query.list();
        for(GroupNotes groupNotes : groupNotesList){
            groupNotes.getGroupNoteHistorySet().clear();
            groupNotes.getMattachments().clear();
            groupNotes.getUnreadNotesSet().clear();
            session.flush();
        }

        org.hibernate.Query query1 = session.createQuery("delete from GroupNotes as " +
                "n where n.ownednotes.msectionid=:msectionid");
        query1.setParameter("msectionid",msectionid);
        query1.executeUpdate();
        session.flush();
        return true;
    }

执行上述代码时出现以下错误:

SEVERE: Servlet.service() for servlet [appServlet] in context with path [] threw exception [Request processing failed; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement] with root cause
org.postgresql.util.PSQLException: ERROR: update or delete on table "groupnotes" violates foreign key constraint "groupnotes_groupnotehistory_fk" on table "groupnotehistory"
  Detail: Key (mnoteid)=(5455) is still referenced from table "groupnotehistory".

不是第一个清除所有集合的查询,应该从数据库中清除历史记录和所有。理想情况下应该没有问题,因为它是级联删除。我还尝试使用query1.list检索List并在for循环中删除,这也无效。我不明白如何解决这个问题。非常感谢。

1 个答案:

答案 0 :(得分:2)

这里有几件事是错误的。首先,调用clear()只处理双向关系的一面。您需要访问每个子项并将其对GroupNote的引用设置为null。你写它的方式,数据库在刷新后将保持不变,不会删除任何实体。

其次,在HQL中调用delete会绕过cascade个选项。因此,即使没有第一个问题,也不会以这种方式删除相关实体,从而导致您看到的异常。