title: Hibernate Many to Many integrity constraint violation
body: <p>I am having trouble with a Many to Many relationship that i have mapped with hibernate!
  I have 3 tables:</p>
  
  <ul>
  <li>Clients - Contain information about a client</li>
  <li>Resources - Contains information about a resource</li>
  <li>ClientAccessResource - Contain information about which resource a client has access to!</li>
  </ul>
  
  <p><img src="https://i.stack.imgur.com/9pPtB.png" alt="My Schema"></p>
  
  <p>Now, everything is working fine except when i try to delete a resource that has a reference to "clients_access_resources", it gives <strong>" Referential integrity constraint violation".</strong>
  But the wierd thing is that, if i delete a Client that has a reference to "clients_access_resource" it works, no problem here!</p>
  
  <p><strong>Client</strong></p>
  
  <pre><code>@ManyToMany(fetch = FetchType.EAGER, targetEntity=Resources.class)
      @JoinTable(name = "clients_access_resources", joinColumns = { @JoinColumn(name = "client_id") }, inverseJoinColumns = { @JoinColumn(name = "res_id") })
      public Set&lt;Resources&gt; getClientResources() {
          return this.clientResources;
      }
  </code></pre>
  
  <p><strong>Resources</strong></p>
  
  <pre><code>@ManyToMany(fetch = FetchType.EAGER, mappedBy = "clientResources")
  public Set&lt;ClientsBasic&gt; getClientsBasics() {
      return this.clientsBasics;
  }
  </code></pre>
  
  <p><strong>Resources DAO</strong></p>
  
  <pre><code>public void delete(Resources res) throws HibernateException{
          synchronized (this) {
              Transaction tx = null;
              session =  this.sessionFactory.openSession();
              tx = session.beginTransaction();
              session.delete(res);
              tx.commit();
              session.close();
          }
      }
  </code></pre>
  
  <p><strong>Clients DAO</strong></p>
  
  <pre><code>public ClientsBasic save(ClientsBasic client) throws HibernateException {
      synchronized (this) {
          Transaction tx = null;
          session = this.sessionFactory.openSession();
          tx = session.beginTransaction();
          session.saveOrUpdate(client);
          tx.commit();
          session.close();
          return client;
      }
  
  }
  </code></pre>
  
  <p>I've tried to add "cascade = CascadeType.ALL" but it didn't work.</p>
  
  <p>Can anyone please help me?
  Thank you.</p>