小编Kir*_*ran的帖子

如何从jt中具有多对多关系的jointable中删除记录

删除条目时,我收到约束违规异常

我有关系表TransportationEvent和结论

关系就像

@Entity
public class TransportationEvent {

...

@ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.PERSIST)
    private List<Conclusion> conclusions = new ArrayList<Conclusion>();

...

}

@Entity
public class Conclusion {

....

@ManyToMany( fetch = FetchType.LAZY, cascade = CascadeType.PERSIST)
    private List<TransportationEvent> transportationEvents = new ArrayList<TransportationEvent>();

....

}
Run Code Online (Sandbox Code Playgroud)

在数据库中,我有另外两个表

结论_TransportationEvent和TransportationEvent_Conclusion

这里我的要求是我需要删除两个表中的记录(TransportationEvent和结论)

在这里,我试图删除如下的结论表记录:

removeConclusions(conclusion.getId());

 public void removeConclusions(Long id) {
        entityManager = dbConn.getConnection();
        Conclusion conclusion = entityManager.find(Conclusion.class, id);
        entityManager.getTransaction().begin();
        entityManager.remove(conclusion);
        entityManager.getTransaction().commit();
}
Run Code Online (Sandbox Code Playgroud)

但我得到约束违规错误.

引发者:java.sql.SQLException:DELETE语句与REFERENCE约束"FK30CDAB072AAE439"冲突.冲突发生在数据库"ECT",表"dbo.TransportationEvent_Conclusion",列'ending_id'

通过搜索一些论坛,我得到了语法

@Cascade(org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
Run Code Online (Sandbox Code Playgroud)

我把它应用为

@ManyToMany(fetch = FetchType.EAGER, cascade = {CascadeType.PERSIST, CascadeType.REMOVE})
    @Cascade(org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
    private …
Run Code Online (Sandbox Code Playgroud)

hibernate jpa jpa-2.0

4
推荐指数
1
解决办法
6528
查看次数

标签 统计

hibernate ×1

jpa ×1

jpa-2.0 ×1