title: Hibernate OneToOne(optional=true) with FetchMode.JOIN try to re-select null values
body: <p>My problem is that hibernate eager loading of OneToOne association execute +1 select for each null relation.</p>
  
  <p>Entity example:</p>
  
  <pre><code>@Entity 
  class SideBlue {
      @Column(nullable = false)
      private Integer timestamp;
  
      @OneToOne(optional=true) 
      @JoinColumn(name="timestamp", referenceColumn="timestamp", insertable = false, updatable = false) 
      SideRed redSide; 
  }
  @Entity 
  class SideRed {
      @Column(nullable = false)
      private Integer timestamp;
  }
  </code></pre>
  
  <p>(It's a legacy database schema, so database modifications is not allowed)</p>
  
  <p>Query example:</p>
  
  <pre><code>CriteriaBuilder builder... CriteriaQuery query...
  Root&lt;SideBlue&gt; root = query.from(SideBlue.class);
  root.fetch(SideBlue_.sideRed, JoinType.LEFT);
  entityManager().createQuery(query).getResultList();
  </code></pre>
  
  <p>The result:
  If all blue side entities has one red side, everything goes correctly, so hibernate only execute one query to the database for whichever entities will be retrieved.</p>
  
  <p>But, if blue side entities has no red side entity associated, hibernate try to find the other side one more time. Hibernate sql comment says '/* load RedSide */ select ...' for each null redSide property.</p>
  
  <p>How can I skip this second select?</p>
  
  <p>The practical problem appears when latency is not extremely low. If I try to select 1million rows, and 1/3 have null 'red sides', the total latency added is a real problem.</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>This is the debug log for the query</p>
  
  <pre><code>10:04:32.812 [main] DEBUG org.hibernate.loader.Loader - Result set row: 0
  10:04:32.815 [main] DEBUG org.hibernate.loader.Loader - Result row: EntityKey[SideBlue#1269721], EntityKey[SideRed#3620564]
  10:04:32.833 [main] DEBUG org.hibernate.loader.Loader - Result set row: 1
  10:04:32.833 [main] DEBUG org.hibernate.loader.Loader - Result row: EntityKey[SideBlue#1269776], null
  </code></pre>
  
  <p>The first row contains the blue and red sides, but the second one only the blue side. So hibernate must know that related red side dont exists. But, after all result rows are processed...</p>
  
  <pre><code>10:04:33.083 [main] DEBUG o.h.engine.internal.TwoPhaseLoad - Resolving associations for [BlueSide#1269721]
  10:04:33.084 [main] DEBUG org.hibernate.loader.Loader - Loading entity: [RedSide#component[timestamp]{timestamp=1338937390}]
  10:04:33.084 [main] DEBUG org.hibernate.SQL - /* load RedSide */ select ...
  ! Nothing really loaded because the previous SQL return empty result set, again !
  10:04:33.211 [main] DEBUG org.hibernate.loader.Loader - Done entity load
  </code></pre>