I get a constraint violation exception by deleting entries
I have TransportationEvent relationship tables and conclusion
attitude is like
@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>(); .... }
here in the database, I have two more tables like
Conclusion_TransportationEvent and TransportationEvent_Conclusion
here is my requirement - I need to delete entries in both tables (TransportationEvent and conclusion)
here I am trying to delete the conclusion table entries, for example below:
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(); }
but I get a constraint violation error.
Called: java.sql.SQLException: the DELETE statement was contrary to the REFERENCE constraint "FK30CDAB072AAE439". The conflict occurred in the database "ECT", the table "dbo.TransportationEvent_Conclusion", in the column "outputs_id"
by browsing some forums, I got syntax like
@Cascade(org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
and i applied it like
@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>();
in both objects although iam gets the same restriction of restrictions
can someone help me exactly how I need to use this to remove entries from the Conclusion and TransportationEvent.
Thanks in advance!