title: JPQL TREAT AS /LEFT OUTER JOIN
body: <p>I'm trying the "TREAT AS" function from JPA 2.1 (eclipselink) and I'm stuck with an error from JPA:</p>
  
  <p>Exception Description: ReportQuery result size mismatch.  Expecting [263], but retrieved [197]</p>
  
  <p>Here is my JPQL query (I change some parts to be more explicit):</p>
  
  <pre><code>String jpql = "select distinct s, accountAD "
              + "from SheetAccountUser s "
              + "left join fetch s.userTarget target "
              + "left join TREAT(target.accounts AS ADAccount) accountAD ";
  </code></pre>
  
  <p>ADAccount is a subclass from AbstractAccount (@Inheritance(strategy = InheritanceType.JOINED).
  A User have a list of AbstractAccount.</p>
  
  <p>I want to select sheets with the AD account of the userTarget. I want null if there is no userTarget or if the userTarget has no AD account (left join).</p>
  
  <p>The problem comes from the treat operator. The SQL generated query have a left join between the AbstractAccount table and the ADAccount table.
  This cause the retrieve of one line per account type of the targetUser.</p>
  
  <p>Here is the SQL query generated:</p>
  
  <pre><code>SELECT DISTINCT 
  t0.Id, --etc
  t6.Id, t6.name, --etc
  t7.userId --etc
  FROM sheet t0 
  LEFT OUTER JOIN user t6 ON (t6.Id = t0.userTargetId),
  account t7 LEFT OUTER JOIN ad_account t8 ON ((t8.userId = t7.userId) AND (t8.idApp = t7.idApp))
  WHERE (t7.userId = t6.Id) AND (t7.DTYPE = 'ADAccount');
  </code></pre>
  
  <p>We can see the left outer join between account and ad_account. 
  Also, the ad_account table is not present in the select clause.
  (The idApp field is a part of the primary key and maintain the unique (userId, idApp) constraint).
  I don't know if it's a problem with my understanding or JPA.</p>
  
  <p>Thank you for helping me! </p>