title: Hibernate many-to-many collection filtering
body: <p>I have the following POJO with a Set inside:</p>
  
  <pre><code>class Word {
      private Long id;
      private String word;
      private int type = WordListFactory.TYPE_DEFAULT;
  
      private Set&lt;Word&gt; refs = new HashSet&lt;Word&gt;();
  ...
  }
  </code></pre>
  
  <p>Here's the mapping XML:</p>
  
  <pre><code>  &lt;class name="kw.word.Word" table="word"&gt;
          &lt;id name="id" column="id"  unsaved-value="null"&gt;
              &lt;generator class="native"/&gt;
          &lt;/id&gt;
  
          &lt;property name="word"
                    unique="true"
                    not-null="true"/&gt;
          &lt;property name="type"/&gt;
  
          &lt;set name="refs"
               table="word_key"
               cascade="save-update"&gt;
  
              &lt;key column="word_id"/&gt;
              &lt;many-to-many class="kw.word.Word" 
                     column="word_ref_id"
                     fetch="join"&gt;                         
              &lt;/many-to-many&gt;                            
          &lt;/set&gt;
  
  
      &lt;/class&gt;
  </code></pre>
  
  <p>There are two tables: <em>word</em> and <em>word_key</em>. The latter links word-parents to word-children.</p>
  
  <p>I'm trying to implement set items filtering when the data is fetched from DB. The resulting object set must contain only items with a specific type.</p>
  
  <p>I tried various things:</p>
  
  <ul>
  <li>Using filtering in mapping like (sorry for lack of brackets)</li>
  </ul>
  
  <pre><code>    
        many-to-many class="kw.word.Word"
                    column="word_ref_id"
                    fetch="join">
           filter name="word_type" condition="type=:type"          
       many-to-many
  </code></pre>
  
  <p>In the code that fetches data I enabled the filter and set the parameter. According to logs hibernate seems to ignore this particular filter as it there's no condition in resulting SQL query.</p>
  
  <ul>
  <li><p>Using additional condition in Criteria</p>
  
  <pre><code>  Word result = null;
  
  session.beginTransaction();
  
  Criteria crit = session.createCriteria(Word.class);       
  
  crit.add(Restrictions.like("word", key))
       .createAlias("refs", "r")
       .add(Restrictions.eq("r.type", getType()));//added alias and restriction for type
  
   List list = crit.list();
  
   if(!list.isEmpty())
       result = list.get(0);   
  
   session.getTransaction().commit();</code></pre></li>
  </ul>
  
  <p>now the resulting SQL seems to be OK</p>
  
  <pre><code>   select
          this_.id as id0_1_,
          this_.word as word0_1_,
          this_.type as type0_1_,
          refs3_.word_id as word1_,
          r1_.id as word2_,
          r1_.id as id0_0_,
          r1_.word as word0_0_,
          r1_.type as type0_0_ 
      from
          word this_ 
      inner join
          word_key refs3_ 
              on this_.id=refs3_.word_id 
      inner join
          word r1_ 
              on refs3_.word_ref_id=r1_.id 
      where
          this_.word like ? 
          and r1_.type=?
  </code></pre>
  
  <p>but right after this query there's another one that fetches all the items</p>
  
  <pre><code> select
          refs0_.word_id as word1_1_,
          refs0_.word_ref_id as word2_1_,
          word1_.id as id0_0_,
          word1_.word as word0_0_,
          word1_.type as type0_0_ 
      from
          word_key refs0_ 
      left outer join
          word word1_ 
              on refs0_.word_ref_id=word1_.id 
      where
          refs0_.word_id=?
  </code></pre>
  
  <p>Maybe I'm doing something wrong?</p>