title: How do I use JPQL to delete entries from a join table?
body: <p>I have a JPA object which has a many-to-many relationship like this:</p>
  
  <pre><code>@Entity
  public class Role {
      //...
  
      @ManyToMany(fetch=FetchType.EAGER)
      @JoinTable(
      name="RolePrivilege",
      joinColumns=
          @JoinColumn(name="role", referencedColumnName="ID"),
      inverseJoinColumns=
          @JoinColumn(name="privilege", referencedColumnName="ID")
      )
      private Set&lt;Privilege&gt; privs;
  }
  </code></pre>
  
  <p>There isn't a JPA object for RolePrivilege, so I'm not sure how to write a JPQL query to delete entries from the privs field of a role object. For instance, I've tried this, but it doesn't work. It complains that Role.privs is not mapped.</p>
  
  <pre><code>DELETE FROM Role.privs p WHERE p.id=:privId
  </code></pre>
  
  <p>I'm not sure what else to try. I could of course just write a native query which deletes from the join table RolePrivilege. But, I'm worried that doing so would interact badly with locally cached objects which wouldn't be updated by the native query.</p>
  
  <p>Is it even possible to write JPQL to remove entries from a join table like this? If not I can just load all the Role objects and remove entries from the privs collection of each one and then persist each role. But it seems silly to do that if a simple JPQL query will do it all at once.</p>