title: Hibernate 2nd level cache objects that are lazy=false, result in a default fetch=join, is it documented anywhere?
body: <p>I experience the following apparently undocumented issue, and I want to understand if </p>
  
  <ol>
  <li>I did something wrong</li>
  <li>Did anyone encounter the same issue?</li>
  <li>Is it really not documented anywhere? or did I miss something?</li>
  </ol>
  
  <p>The behavior is this
  Assume the following mapping</p>
  
  <pre><code>&lt;class name="org.sample.Foo" table="foo"&gt;
      ...
     &lt;many-to-one name="bar" class="org.sample.Bar"/&gt;
  &lt;/class&gt;
  
  
  &lt;class name="org.sample.Bar" table="bar" lazy="false"&gt;
      ...
  &lt;/class&gt;
  </code></pre>
  
  <p>First, as a background, Hibernate default value for the <em>fetch</em> attribute on a many-to-one relation should be "<em>select</em>", this is at least what is documented (I'll add the link here when I find it)</p>
  
  <p>However, this is apparently only true if the referenced class is lazy="true"!</p>
  
  <p>so apparently the above mapping is translated into this (because Bar is lazy="false"):</p>
  
  <pre><code>&lt;class name="org.sample.Foo" table="foo"&gt;
      ...
     &lt;many-to-one name="bar" class="org.sample.Bar" *fetch="join" /&gt;
  &lt;/class&gt;
  
  
  &lt;class name="org.sample.Bar" table="bar" lazy="false"&gt;
      ...
  &lt;/class&gt;
  </code></pre>
  
  <p>Now why would that be an issue? instead of 2 selects, Hibernate will load the non lazy reference in a single select with its "parent" (load Foo with Bar in a single select)</p>
  
  <p>this actually makes sense, since the object is not lazy, why not load it?</p>
  
  <p>The answer is this: what happens if Bar is in the 2nd level cache?</p>
  
  <pre><code>&lt;class name="org.sample.Foo" table="foo"&gt;
      ...
     &lt;many-to-one name="bar" class="org.sample.Bar" *fetch="join" /&gt;
  &lt;/class&gt;
  
  
  &lt;class name="org.sample.Bar" table="bar" lazy="false"&gt;
      &lt;cache usage="transactional" /&gt;
      ...
  &lt;/class&gt;
  </code></pre>
  
  <p>And the answer to that is - nothing changes!</p>
  
  <p>Apparently one would assume Hibernate is smart enough to understand that objects of this type should not be loaded, but since the default fetch was changed from select to join, Hibernate doesn't have a choice (you can't join a real table with the 2nd level cache, yet)</p>
  
  <p>so Hibernate does what it is told, and uses a join to fetch an object from the database where it is already in the 2nd level cache</p>
  
  <p>The solution I found is to literally change the mapping to fetch="select"</p>
  
  <p>Now when the second select for Bar is about to go, Hibernate understands it should not go to the database, and fetches it from the cache. and only 1 query will execute (after warmup)</p>