Hello All, I am getting the constraint violation exception whil deleting the entries
I have Relation tables TransportationEvent and Conclusion , relations 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 database i have got another two table like
Conclusion_TransportationEvent and TransportationEvent_Conclusion
here my requirement is i need to delete records in both tables (TransportationEvent and Conclusion)
here i am trying to delete conclusion table records like bellow:
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 iam getting constraint violation error .
Caused by: java.sql.SQLException: The DELETE statement conflicted with the REFERENCE constraint "FK30CDAB072AAE439". The conflict occurred in database "ECT", table "dbo.TransportationEvent_Conclusion", column 'conclusions_id'
by searching some forums i got syntax like
@Cascade(org.hibernate.annotations.CascadeType.DELETE_ORPHAN) and i applied that as @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 entities even though iam geting same constraint violation
can some one help me how exactly i need to use this to delete the records from Conclusion and TransportationEvent.
Thanks in advance!