title: hibernate StatelessSession update with many-to-one not working (transient instance)?
body: <p>When I retrieve a Entity (row) with an intentionally unfetched many-to-one association and then try to update that Entity I get the famous transient instance error:</p>
  
  <p><strong>Error updating Object
  object references an unsaved transient instance - save the transient instance before flushing</strong></p>
  
  <p>But that "transient instance" was never fetched and so cannot even be transient</p>
  
  <p><strong>Code:</strong> </p>
  
  <p>ProductFamily - java code</p>
  
  <pre><code>public class ProductFamily
  {
      private Integer id;
      private Company company;
      private String name;
  
      //autogenerated getter &amp; setter and constructors
  }
  </code></pre>
  
  <p>ProductFamily - hbm.xml</p>
  
  <pre><code>&lt;hibernate-mapping&gt;
      &lt;class name="package.ProductFamily" table="product_family" catalog="db"&gt;
          &lt;id name="id" type="java.lang.Integer"&gt;
              &lt;column name="id" /&gt;
              &lt;generator class="identity" /&gt;
          &lt;/id&gt;
          &lt;many-to-one name="company" class="package.Company" fetch="select"&gt;
              &lt;column name="company" not-null="true"/&gt;
          &lt;/many-to-one&gt;
          &lt;property name="name" type="string"&gt;
              &lt;column name="name" length="64" not-null="true"/&gt;
          &lt;/property&gt;
      &lt;/class&gt;
  &lt;/hibernate-mapping&gt;
  </code></pre>
  
  <p>Company - java code</p>
  
  <pre><code>public class Company
  {
      private String name;
      private Set&lt;ProductFamily&gt; productFamilies = new HashSet&lt;ProductFamily&gt;(0);
      //autogenerated getter &amp; setter and constructors
  }
  </code></pre>
  
  <p>Company - hbm.xml   </p>
  
  <pre><code>&lt;!-- same header as above --&gt;
  &lt;hibernate-mapping&gt;
      &lt;class name="package.Company" table="company" catalog="db"&gt;
          &lt;id name="id" type="java.lang.Integer"&gt;
              &lt;column name="id" /&gt;
              &lt;generator class="identity" /&gt;
          &lt;/id&gt;
          &lt;property name="name" type="string"&gt;
              &lt;column name="name" length="64" not-null="true"/&gt;
          &lt;/property&gt;
          &lt;set name="productFamilies" table="product_family" inverse="true" lazy="true" fetch="select"&gt;
              &lt;key&gt;
                  &lt;column name="company" not-null="true"/&gt;
              &lt;/key&gt;
              &lt;one-to-many class="package.ProductFamily" /&gt;
          &lt;/set&gt;
      &lt;/class&gt;
  &lt;/hibernate-mapping&gt;
  </code></pre>
  
  <p>test method simple retrieve and change and update</p>
  
  <pre><code>public void test()
  {
      final StatelessSession session = this.sessionFactory.openStatelessSession();
  
      //get
      final ProductFamily productFamily = (ProductFamily)session.get(ProductFamily.class, 1);
  
      //change primitive property
      productFamily.setName(productFamily.getName() + "x");
  
      //update
      final Transaction tr = session.beginTransaction();
  
      try
      {
          session.update(productFamily);
          tr.commit();
      }
      catch (HibernateException e)
      {
          logger.severe(e.getMessage() + "\n" + e.getStackTrace());
          try
          {
              if(tr != null
                  &amp;&amp; tr.isActive())
                  tr.rollback();
          }
          catch (HibernateException eTr)
          {
              logger.severe(eTr.getMessage() + "\n" + eTr.getStackTrace());
          }
      }
      finally
      {
          session.close();
      }
  }
  </code></pre>