title: How to add a where clause to a Hibernate @ManyToMany relationship?
body: <p>Given two entities:</p>
  
  <ul>
  <li><code>PurchaseProductGroup</code></li>
  <li><code>PurchaseProduct</code>, which has a <code>status</code> column that can be <code>'A'</code> (active) or <code>'D'</code> (deleted)</li>
  </ul>
  
  <p>and a many-to-many relationship defined in <code>PurchaseProductGroup</code>:</p>
  
  <pre><code>/** The purchase products linked to this group. */
  @ManyToMany
  @JoinTable(name = "purchaseprodgrp_purchaseprod",
             joinColumns = @JoinColumn(name = "ppg_id"),
             inverseJoinColumns = @JoinColumn(name = "ppr_id"))
  private List&lt;PurchaseProduct&gt; purchaseProducts;
  </code></pre>
  
  <p>How can I restrict this so that purchaseProducts with a status of <code>'D'</code> are excluded?</p>
  
  <h2>Things I've tried</h2>
  
  <p>Have tried adding the following just below the <code>@ManyToMany</code> annotation but both failed with an "invalid identifier" exception for the column:</p>
  
  <ol>
  <li><code>@Where(clause = "status &lt;&gt; 'D'")</code></li>
  <li><code>@WhereJoinTable(clause = "purchaseProduct.status &lt;&gt; 'D'")</code></li>
  </ol>
  
  <p>Also tried using adding <code>@Where(clause = "status &lt;&gt; 'D'")</code> at the entity level but this doesn't seem to affect the contents of relationship collections - as backed up by <a href="https://stackoverflow.com/questions/7892665">this question</a>.</p>