title: How to work around Session.get() strange behaviour with composite-id?
body: <p>I'm trying to perform get on the session for object which declares composite id without a mapped composite identifier.<br>
  Hibernate version used is 3.5.5.</p>
  
  <p>Fetching code is generic and reads container objects wrapping actual data:</p>
  
  <pre><code>ClassMetadata metadata = 
            session.getSessionFactory().getClassMetadata(wrapper.getDomainClass());
  Serializable id = metadata.getIdentifier(wrapper, EntityMode.POJO);
  return session.get(wrapper.getDomainClass(), id, LockOptions.UPGRADE);
  </code></pre>
  
  <p>Code doesn't know anything about actual mapping so it has to consult metadata about the id.</p>
  
  <p>If mapping is defined like:</p>
  
  <pre><code>&lt;hibernate-mapping default-access="field"&gt;
    &lt;class name="Wrapper"
        entity-name="Data"
        table="DATA"&gt;
      &lt;composite-id&gt;
        &lt;key-property name="identifier" column="identifier" /&gt;
        &lt;key-property name="version" column="version" /&gt;
      &lt;/composite-id&gt;
  
      &lt;component name="domainObject" class="Data"&gt;
        &lt;property name="source" column="source" /&gt;
      &lt;/component&gt;                    
    &lt;/class&gt;
  &lt;/hibernate-mapping&gt;
  </code></pre>
  
  <p>without composite identifier class, id is equal to the object itself and is equal to wrapper reference.<br>
  When I do session.get() instead of fetching object from the database, it returns back same object as was passed in id (not an equal object, but same instance of object).<br>
  <strong>Upd:</strong> In fact session.get() loads object from database <code>in</code> the id object passed and returns it back. I oversaw that initially thinking it skips loading.</p>
  
  <p>Solution that I found so far is introduce mapped composite identifier and change mapping to:</p>
  
  <pre><code>&lt;hibernate-mapping default-access="field"&gt;
    &lt;class name="Wrapper"
        entity-name="Data1"
        table="DATA_1"&gt;
      &lt;composite-id  class="SurrogateKey" mapped="true"&gt;
        &lt;key-property name="identifier" column="identifier" /&gt;
        &lt;key-property name="version" column="version" /&gt;
      &lt;/composite-id&gt;
  
      &lt;component name="domainObject" class="Data"&gt;
        &lt;property name="source" column="source" /&gt;
      &lt;/component&gt;                    
    &lt;/class&gt;
  &lt;/hibernate-mapping&gt;
  </code></pre>
  
  <p>SurrogateKey is defined as object with two fields and equals/hashcode as required.<br>
  With this change id returned by metadata.getIdentifier() is an instance of SurrogateKey and session.get() fetches object from the database if it exists.</p>
  
  <p>The problem with mapping fix is that property names for criteria and HQL change from <strong>identifier</strong> to <strong>id.identifier</strong> and that is actually breaking a lot of existing code.</p>
  
  <p>Things I'm exploring at the moment are:</p>
  
  <ol>
  <li>Is there a way to make session.get() work without declaring Id class (I know this is a discouraged practice, but amount of changes needed might be prohibitive)?</li>
  <li>Could the alternative be telling hibernate to treat properties as before, without adding id. in front of them?</li>
  <li>Upgrade hibernate to v4 (not easy because of dependent projects and approval process)?</li>
  <li>Any other options/workarounds available?</li>
  </ol>
  
  <p>So far I've only managed to make a solution described above work, but I'm searching for less intrusive one and would appreciate any clues, suggestions, pointers to relevant docs.</p>