title: Using inverse true in hibernate
body: <p>I am going through the hibernate documentation and came across the concept of inverse attribute. I am new to Hibernate so I am feeling difficulty in understanding the concept properly.</p>
  
  <p><a href="http://docs.jboss.org/hibernate/orm/3.3/reference/en-US/html/collections.html#collections-bidirectional">http://docs.jboss.org/hibernate/orm/3.3/reference/en-US/html/collections.html#collections-bidirectional</a></p>
  
  <pre><code>&lt;class name="Category"&gt;
      &lt;id name="id" column="CATEGORY_ID"/&gt;
      ...
      &lt;bag name="items" table="CATEGORY_ITEM"&gt;
          &lt;key column="CATEGORY_ID"/&gt;
          &lt;many-to-many class="Item" column="ITEM_ID"/&gt;
      &lt;/bag&gt;
  &lt;/class&gt;
  
  &lt;class name="Item"&gt;
      &lt;id name="id" column="ITEM_ID"/&gt;
      ...
  
      &lt;!-- inverse end --&gt;
      &lt;bag name="categories" table="CATEGORY_ITEM" inverse="true"&gt;
          &lt;key column="ITEM_ID"/&gt;
          &lt;many-to-many class="Category" column="CATEGORY_ID"/&gt;
      &lt;/bag&gt;
  &lt;/class&gt;
  </code></pre>
  
  <p>From above code, the <code>inverse="true"</code> is applied to categories, so I understood that categories is the inverse end.</p>
  
  <p>But I am seeing some contradiction to my understanding:</p>
  
  <blockquote>
    <p>Changes made only to the inverse end of the association are not
    persisted.</p>
  </blockquote>
  
  <pre><code>category.getItems().add(item);   // The category now "knows" about the relationship
  item.getCategories().add(category); // The item now "knows" about the relationship
  
  session.persist(item);   // The relationship won't be saved!
  session.persist(category);   // The relationship will be saved
  </code></pre>
  
  <p>If categories is on inverse end then how the relationship is saved here?</p>
  
  <blockquote>
    <p>The non-inverse side is used to save the in-memory representation to
    the database.</p>
  </blockquote>
  
  <p>After looking at the example and reading above statement I came to know that categories is on non-inverse end.</p>
  
  <p>Please help me in knowing how to interpret this <code>inverse="true"</code> attribute. After searching in net and looking at answers in SO, I came to know the usefulness of this attribute but still I have this confusion.</p>