JPA ManyToMany正在删除

时间:2013-11-29 16:14:42

标签: java hibernate jpa many-to-many

我有两个班级:

public class Keyword {
  @ManyToMany(cascade = CascadeType.ALL, mappedBy = "keywords")
  private Set<Thesis> theses = new HashSet<Thesis>();
}


public class Thesis {
    @ManyToMany(cascade = CascadeType.ALL)
    private Set<Keyword> keywords = new HashSet<Keyword>();
}

现在我希望能够删除关键字并删除没有关联对象的论文。我怎么这么做?我试过@OnDelete(action = OnDeleteAction.NO_ACTION),没用,我试过

@PreRemove
void onPreRemove() {

    this.getTheses().clear();
    this.persist();
    this.flush();
}

并没有成功。这样做的正确方法是什么?

好的,联系我的解决方案以设置Set&lt;&gt;没有用到null。这样它接缝就可以了:

@PreRemove
void onPreRemove() {

    log.debug("in preRemove");

    //this.getTheses().clear();
    for (Thesis s : this.getTheses()) {
        s.getKeywords().remove(this);
        s.persist();
        s.flush();
    }
    this.persist();
    this.flush();
    entityManager.flush();
}

0 个答案:

没有答案