title: Is JPQL not the way to go for projections?
body: <p>Here's my Repository method that I'm trying to call.</p>
  
  <pre><code>@Query(value = "select ise from InquirySession ise " +
          "inner join ise.inquiryRequest.inquiryRequestFacilities irf " +
          "left outer join ise.inquiryResponses ire on ire.facilityId = ?2 " +
          "left outer join ire.responseActions ira " +
          "left outer join ire.acceptDates irad " +
          "left outer join ire.responseReasons irr " +
          "where ise.carePathItem.id = ?1 and irf.facilityId = ?2")
      Set&lt;InquirySession&gt; getAllByCarePathItemIdAndProviderId(final Long carePathItemId, final Long providerId);
  </code></pre>
  
  <p>My thought process was that when this method is executed, <em>ise.inquiryResponses</em> would be joined to InquirySession using the additional 'on' clause defined in the JPQL. However, I think this JPQL only selects InquirySession and does separate calls to join all other entities (defined in InquirySession) as I see separate sql statements after this one joining each entity using the primary/foreign key relationship.</p>
  
  <p>Here's how InquiryResponse looks inside InquirySession</p>
  
  <pre><code>@OneToMany(mappedBy = "inquirySessionId")
  @OrderBy(value = "createDate")
  private Set&lt;InquiryResponse&gt; inquiryResponses;
  </code></pre>
  
  <p>and vice versa,</p>
  
  <pre><code>@Column(name = "inquirySessionDatabaseKey")
  private Long inquirySessionId;
  </code></pre>
  
  <p>Is it possible to join InquiryResponse with the additional <em>on</em> clause ire.facilityId = ?2 when hydrating InquirySession entity using JPQL?</p>
  
  <p><strong>Edit</strong></p>
  
  <p>JPQL equivalent SQL</p>
  
  <pre><code>select inquiryses0_.inquirySessionId as inquirySessionId1_36_, inquiryses0_.carepathitemid as CarePat12_36_, inquiryses0_.createDate as createDa2_36_, inquiryses0_.description as descript3_36_, 
  inquiryses0_.fileSize as fileSize4_36_, inquiryses0_.documentLink as inquiryS5_36_, inquiryses0_.numberOfPages as numberOf6_36_, inquiryses0_.patientId as patientId7_36_, 
  inquiryses0_.patientEncounterId as patientV8_36_, inquiryses0_.patientplannerid as patientplannerid13_36_, inquiryses0_.purgedOn as purgedOn9_36_, inquiryses0_.amazonbucketname as s10_36_, 
  inquiryses0_.type as type11_36_ 
  from InquirySession inquiryses0_ 
  inner join InquiryRequest inquiryreq1_ on inquiryses0_.inquirySessionId=inquiryreq1_.inquirySessionId 
  inner join InquiryRequest_Facility inquiryreq2_ on inquiryreq1_.inquiryrequestid=inquiryreq2_.inquiryrequestid 
  left outer join InquiryResponse inquiryres3_ on inquiryses0_.inquirySessionId=inquiryres3_.inquirySessionId and (inquiryres3_.providerId=?) 
  left outer join InquiryResponse_Action_Link inquiryres3_1_ on inquiryres3_.inquiryresponseid=inquiryres3_1_.inquiryresponseid 
  left outer join InquiryResponseActions inquiryres4_ on inquiryres3_1_.inquiryresponseactionid=inquiryres4_.inquiryresponseactionid 
  left outer join InquiryResponse_AcceptDate_Link acceptdate5_ on inquiryres3_.inquiryresponseid=acceptdate5_.inquiryresponseid 
  left outer join InquiryResponse_Reason_Link responsere6_ on inquiryres3_.inquiryresponseid=responsere6_.inquiryresponseid 
  left outer join InquiryResponseReasons inquiryres7_ on responsere6_.responsereasonid=inquiryres7_.responsereasonid 
  where inquiryses0_.carepathitemid=? and inquiryreq2_.providerId=?
  </code></pre>
  
  <p>SQL that does not honor the 'on' clause and joins just on InquirySession.inquirySessionId</p>
  
  <pre><code>select inquiryres0_.inquirySessionId as inquirySessionId5_30_0_, inquiryres0_.inquiryresponseid as inquiryresponseid1_30_0_, inquiryres0_.inquiryresponseid as inquiryresponseid1_30_1_, inquiryres0_.createDate as createDa2_30_1_, 
  inquiryres0_.providerId as providerId3_30_1_, inquiryres0_.alsoCallMe as alsoCall4_30_1_, inquiryres0_.inquirySessionId as inquirySessionId5_30_1_, inquiryres0_.phoneNumber as phoneNum6_30_1_, 
  inquiryres0_.respondedBy as responde7_30_1_, inquiryres0_.responseText as response8_30_1_, inquiryres0_.responseTypeID as response9_30_1_, 
  inquiryres0_1_.inquiryresponseactionid as RSPNS_AC1_32_1_, inquiryres1_.inquiryresponseactionid as RSPNS_AC1_34_2_, inquiryres1_.inquiryresponseactiondescription as RSPNS_AC2_34_2_ 
  from InquiryResponse inquiryres0_ 
  left outer join InquiryResponse_Action_Link inquiryres0_1_ on inquiryres0_.inquiryresponseid=inquiryres0_1_.inquiryresponseid 
  inner join InquiryResponseActions inquiryres1_ on inquiryres0_1_.inquiryresponseactionid=inquiryres1_.inquiryresponseactionid 
  where inquiryres0_.inquirySessionId=? order by inquiryres0_.createDate
  </code></pre>
  
  <p>Looking at the first SQL, it's quite evident that the JPQL does not really care about joining other entities in InquirySession as all of the select statements are for InquirySession table. It does a separate <em>SELECT</em> statement to join InquiryResponse and thus, no longer has the expected additional <em>ON</em> clause.</p>