title: Criteria query - setFetchMode vs createAlias, n+1 selects
body: <p>I have a bidirectional one-to-one association between Car and Engine mapped as follows: (with Hibernate 3.6)</p>
  
  <pre><code>&lt;class name="org.example.Car"&gt;
      &lt;id column="id" name="PID"&gt;
          &lt;generator class="uuid2" /&gt;
      &lt;/id&gt;
      &lt;version name="Version" column="version" /&gt;
  
      &lt;many-to-one cascade="all" name="Engine" not-null="true" unique="true" /&gt;
  &lt;/class&gt;
  &lt;class name="org.example.Engine"&gt;
      &lt;id column="id" name="PID"&gt;
          &lt;generator class="uuid2" /&gt;
      &lt;/id&gt;
      &lt;version name="Version" column="version" /&gt;
  
      &lt;one-to-one cascade="all" name="Car" property-ref="Engine" /&gt;
  &lt;/class&gt;
  </code></pre>
  
  <p>When I perform the following criteria query to load all Cars and eager fetch their Engines</p>
  
  <pre><code>session().createCriteria(Car.class)
     .setFetchMode("Engine", FetchMode.JOIN)
     .list();
  </code></pre>
  
  <p>I get one select to find all Cars, and then another select for each Car that was found:</p>
  
  <pre><code>select this_.id  ... from Car this_ inner join Engine engine2_ on this_.Engine=engine2_.id
  select car0_.id  ... from Car car0_ where car0_.engine=?
  select car0_.id  ... from Car car0_ where car0_.engine=?
  select car0_.id  ... from Car car0_ where car0_.engine=?
  ...
  </code></pre>
  
  <p>It appears that Hibernate gets confused when setting the properties on each <code>Engine</code> that was loaded. In <code>TwoPhaseLoad.initializeEntity</code>, it attempts to resolve the Car property on <code>Engine</code> and issues another select.</p>
  
  <p>If use createAlias instead of setFetchMode, I get the expected single select and everything works:</p>
  
  <pre><code>session().createCriteria(Car.class)
     .createAlias("Engine", "engine")
     .list();
  </code></pre>
  
  <p>I was under the impression that those two criteria queries should behave the same. Is there something I can change in my mapping? Should I start using createAlias instead of setFetchMode?</p>
  
  <p><strong>UPDATE:</strong> So, the second query isn't good either. It actually joins Engine and then joins Car again for the back-reference:</p>
  
  <pre><code>select this._id  ... from Car this_
     inner join Engine engine1_ on this_.engine=engine1_.id
     left outer join Car car3_ on engine1_.id=car3_.engine
  </code></pre>
  
  <p>It doesn't seem like hibernate realizes they are 2 ends of the same association. Removing the reference from Engine -> Car fixes it but it would be nice if I could get this to work while keeping the reference.</p>