title: How to remove records from jointable which has a many to many relation in jpa
body: <p>I am getting the constraint violation exception whil deleting the entries</p>
  
  <p>I have Relation tables    TransportationEvent  and Conclusion </p>
  
  <p>relation islike</p>
  
  <pre><code>@Entity
  public class TransportationEvent {
  
  ...
  
  @ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.PERSIST)
      private List&lt;Conclusion&gt; conclusions = new ArrayList&lt;Conclusion&gt;();
  
  ...
  
  }
  
  @Entity
  public class Conclusion {
  
  ....
  
  @ManyToMany( fetch = FetchType.LAZY, cascade = CascadeType.PERSIST)
      private List&lt;TransportationEvent&gt; transportationEvents = new ArrayList&lt;TransportationEvent&gt;();
  
  ....
  
  }
  </code></pre>
  
  <p>here in database i have got another two table like</p>
  
  <p>Conclusion_TransportationEvent     and  TransportationEvent_Conclusion</p>
  
  <p>here my requirement is i need to delete  records in both tables (TransportationEvent and Conclusion)</p>
  
  <p>here i am trying to delete conclusion table records like bellow:</p>
  
  <pre><code>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();
  }
  </code></pre>
  
  <p>but i am getting constraint violation error .</p>
  
  <p>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'</p>
  
  <p>by searching some forums i got syntax like</p>
  
  <pre><code>@Cascade(org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
  </code></pre>
  
  <p>and i applied that as</p>
  
  <pre><code>@ManyToMany(fetch = FetchType.EAGER, cascade = {CascadeType.PERSIST, CascadeType.REMOVE})
      @Cascade(org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
      private List&lt;Conclusion&gt; conclusions = new ArrayList&lt;Conclusion&gt;();
  
  @ManyToMany( fetch = FetchType.LAZY, cascade = CascadeType.ALL)
      @Cascade(org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
      private List&lt;TransportationEvent&gt; transportationEvents = new ArrayList&lt;TransportationEvent&gt;();
  </code></pre>
  
  <p>in both entities even though iam geting same constraint violation</p>
  
  <p>can some one help me how exactly i need to use this to delete the records from Conclusion and TransportationEvent.</p>
  
  <p>Thanks in advance!</p>