title: Inconsistant Indexing while having manytomany relationship through join-table in hibernate search
body: <p>I have indexing problem with lucene through hibernate search:</p>
  
  <p>Product Domain:</p>
  
  <pre><code>    @Entity
      @Table(name="T_PRD")
      @Indexed
      public class Product implements java.io.Serializable {
      @ManyToMany(fetch = FetchType.LAZY)
      @JoinTable(name = "T_TMPL_ATTR_VALUE", joinColumns = { @JoinColumn(name = "EntityId", unique=false) }, inverseJoinColumns = { @JoinColumn(name = "AttrId", unique=false) }, uniqueConstraints=@UniqueConstraint(columnNames={"EntityId","AttrId"}))
      @IndexedEmbedded(prefix="prd.ca.")
      private Set&lt;TemplateAttribute&gt; customTemplateAttributes;
  }
  </code></pre>
  
  <p>TemplateAttribute Domain:</p>
  
  <pre><code>@Entity
  @Table(name = "T_TMPL_ATTRS")
  public class TemplateAttribute implements java.io.Serializable {
  
      @OneToMany(cascade = CascadeType.REMOVE, fetch = FetchType.LAZY, mappedBy = "templateAttribute")
      @IndexedEmbedded
      private Set&lt;TemplateAttributeValue&gt; templateAttrValues = new HashSet&lt;TemplateAttributeValue&gt;(0);
  
      @ManyToMany(fetch = FetchType.LAZY)
      @JoinTable(name = "T_TMPL_ATTR_VALUE", joinColumns = { @JoinColumn(name = "AttrId", unique=false) }, inverseJoinColumns = { @JoinColumn(name = "EntityId", unique=false)}, uniqueConstraints=@UniqueConstraint(columnNames={"EntityId","AttrId"}))
  
  
      @ContainedIn
      private Set&lt;Product&gt; product;
  
  @Column(name = "DefaultValue")
  private String defaultValue;
  
  }
  </code></pre>
  
  <p>TemplateAttributeValue Domain (<strong>Bridge table</strong>, which have <strong>attrValue</strong> field to contain product specific value):</p>
  
  <pre><code>@Entity
  @Table(name = "T_TMPL_ATTR_VALUE")
  public class TemplateAttributeValue implements java.io.Serializable {
  
      @DocumentId(name="id")
      @EmbeddedId
      @AttributeOverrides( {
      @AttributeOverride(name = "entityId", column = @Column(name = "EntityId", nullable = false)),
      @AttributeOverride(name = "attrId", column = @Column(name = "AttrId", nullable = false)) })
      @FieldBridge(impl = TemplateAttributeCompositeKeyBridge.class)
      public TemplateAttributeValueId getId() {
          return this.id;
      }
  
      @ManyToOne(fetch = FetchType.EAGER)
      @JoinColumn(name = "AttrId", nullable = false, insertable = false, updatable = false)
      @ContainedIn
      public TemplateAttribute getTemplateAttribute() {
          return this.templateAttribute;
      }
  
      @Column(name = "AttrValue")
      @Field(index = Index.YES, analyze = Analyze.YES, norms = Norms.NO, store = Store.NO)
      public String getAttrValue() {
          return this.attrValue==null?"":this.attrValue;
      }
  }
  </code></pre>
  
  <p>I called doIndex methos as below:</p>
  
  <pre><code>private void doIndex() throws InterruptedException {
      Session session = getSessionFactory().openSession();
      session.enableFilter("onlyProductTemplate");
      FullTextSession fullTextSession = Search.getFullTextSession(session);
      fullTextSession.createIndexer().startAndWait();
  
      fullTextSession.close();
  }
  </code></pre>
  
  <p>So let's assume below data for example:</p>
  
  <pre><code>Product:
  
  ID | Name
  ----------
   1 | Prd1
   2 | Prd2
   3 | Prd3
  
  Template Attribute:
  
  AttrId | DefaultValue
  ---------------------
  1 | Template1
  2 | Template2
  
  
  Template Attribute Value:
  
  AttrId | EntityId | AttrValue
  -----------------------------
  1 | 1 | Template1
  2 | 1 | Template2_modified
  1 | 2 | Template1Updated
  1 | 2 | Template2
  1 | 3 | Template1
  2 | 3 | Template2_newValue
  </code></pre>
  
  <p>So when i create index for Product Entity, each product generate indexe <strong>all AttrValue</strong> defined in <strong>Template Attribute Value</strong>.</p>
  
  <p>Due to this when seaching for specific product have some matching any template attribute value, it returns all product in list.</p>
  
  <p>So how to resolve this problem in hibernate search.</p>