title: Understand different behaviours of Hibernate/JPA for findAll and findOne
body: <p>I was debugging a performance issue with a findAll method (from a Spring Data JPA Repository) and the problem was the fetch plan that was being used. (Hibernate was performing additional Selects to retrieve the additional entities instead of using a fetch join)</p>
  
  <p>My entity has some relations to another entities and the problem that I was facing was related with the *ToOne relations. </p>
  
  <p>After spending some time on google, I realised that there are some ways to manage the queries that Hibernate will perform (using JPA Criteria, NamedEntityGraph, custom queries, ...). </p>
  
  <p>I've created a small example to demonstrate the different behaviours but I would like to <strong>understand why Hibernate is doing that by default</strong>. I spent some time searching in the docs but I wasn't able to find any reference to this default behaviour.</p>
  
  <p><a href="https://github.com/pmvilaca/jpa-test" rel="nofollow">https://github.com/pmvilaca/jpa-test</a></p>
  
  <p>The difference:</p>
  
  <pre><code>Hibernate: 
  SELECT
    user0_.id                 AS id1_1_0_,
    userdetail1_.id           AS id1_21_,
    contactdet2_.id           AS id1_0_2_,
    user0_.contact_details_id AS contact_3_1_0_,
    user0_.name               AS name2_1_0_,
    userdetail1_.nationality  AS national2_2_1_,
    userdetail1_.user_id      AS user_id3_2_1_,
    contactdet2_.email        AS email2_0_2_,
    contactdet2_.mobile       AS mobile3_0_2_
  FROM user user0_
    LEFT OUTER JOIN user_details userdetail1_ ON user0_.id = userdetail1_.user_id
    LEFT OUTER JOIN contact_details contactdet2_ ON user0_.contact_details_id = contactdet2_.id
  </code></pre>
  
  <p>and</p>
  
  <pre><code>Hibernate: select user0_.id as id1_1_, user0_.contact_details_id as contact_3_1_, user0_.name as name2_1_ from user user0_
  Hibernate: select contactdet0_.id as id1_0_0_, contactdet0_.email as email2_0_0_, contactdet0_.mobile as mobile3_0_0_ from contact_details contactdet0_ where contactdet0_.id=?
  Hibernate: select userdetail0_.id as id1_2_2_, userdetail0_.nationality as national2_2_2_, userdetail0_.user_id as user_id3_2_2_, user1_.id as id1_1_0_, user1_.contact_details_id as contact_3_1_0_, user1_.name as name2_1_0_, contactdet2_.id as id1_0_1_, contactdet2_.email as email2_0_1_, contactdet2_.mobile as mobile3_0_1_ from user_details userdetail0_ left outer join user user1_ on userdetail0_.user_id=user1_.id left outer join contact_details contactdet2_ on user1_.contact_details_id=contactdet2_.id where userdetail0_.user_id=?
  Hibernate: select contactdet0_.id as id1_0_0_, contactdet0_.email as email2_0_0_, contactdet0_.mobile as mobile3_0_0_ from contact_details contactdet0_ where contactdet0_.id=?
  Hibernate: select userdetail0_.id as id1_2_2_, userdetail0_.nationality as national2_2_2_, userdetail0_.user_id as user_id3_2_2_, user1_.id as id1_1_0_, user1_.contact_details_id as contact_3_1_0_, user1_.name as name2_1_0_, contactdet2_.id as id1_0_1_, contactdet2_.email as email2_0_1_, contactdet2_.mobile as mobile3_0_1_ from user_details userdetail0_ left outer join user user1_ on userdetail0_.user_id=user1_.id left outer join contact_details contactdet2_ on user1_.contact_details_id=contactdet2_.id where userdetail0_.user_id=?
  Hibernate: select userdetail0_.id as id1_2_2_, userdetail0_.nationality as national2_2_2_, userdetail0_.user_id as user_id3_2_2_, user1_.id as id1_1_0_, user1_.contact_details_id as contact_3_1_0_, user1_.name as name2_1_0_, contactdet2_.id as id1_0_1_, contactdet2_.email as email2_0_1_, contactdet2_.mobile as mobile3_0_1_ from user_details userdetail0_ left outer join user user1_ on userdetail0_.user_id=user1_.id left outer join contact_details contactdet2_ on user1_.contact_details_id=contactdet2_.id where userdetail0_.user_id=?
  </code></pre>
  
  <p>Any idea?</p>
  
  <p>Thanks</p>