我在删除条目时遇到约束违规异常
我有关系表 TransportEvent 和结论
关系就像
@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>();
....
}
在数据库中,我还有另外两个表,例如
结论_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();
}
但我收到约束违规错误。
原因:java.sql.SQLException:DELETE 语句与 REFERENCE 约束“FK30CDAB072AAE439”冲突。冲突发生在数据库“ECT”、表“dbo.TransportationEvent_Conclusion”、列“conclusions_id”中
通过搜索一些论坛,我得到了类似的语法
@Cascade(org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
我将其应用为
@ManyToMany(fetch = FetchType.EAGER, cascade = {CascadeType.PERSIST, CascadeType.REMOVE})
@Cascade(org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
private List<Conclusion> conclusions = new ArrayList<Conclusion>();
@ManyToMany( fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@Cascade(org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
private List<TransportationEvent> transportationEvents = new ArrayList<TransportationEvent>();
在两个实体中,即使我得到相同的约束违规
有人可以帮助我如何准确地使用它来从结论和运输事件中删除记录。
提前致谢!