title: EclipseLink inheritance queries: Avoid joining
body: <p>Right now, In a short example, if i have two entities <code>Person</code> and <code>Applicant</code> and i want to write a query that gets all applicants in my database, i end up getting referenced queries with the following set up. I am using Single_Table_Inheritance strategy (fyi)</p>
  
  <pre><code>public class Person {
  
      @ManyToOne(fetch = FetchType.LAZY) 
      @JoinColumn(name = "schoolID")
      private School school;
  }
  </code></pre>
  
  <p>The Teacher object with a reference to another entity.</p>
  
  <pre><code>public class Applicant extends Person {
  
    @OneToMany(targetEntity = ApplicantSchool.class, cascade = { javax.persistence.CascadeType.PERSIST, javax.persistence.CascadeType.REMOVE }, mappedBy = "applicant")
      private Set&lt;ApplicantSchool&gt; schools = new HashSet&lt;ApplicantSchool&gt;();
  }
  </code></pre>
  
  <p>Query attempt that jumps and attempts to query for the school relationship in Person</p>
  
  <pre><code> public List&lt;Applicant&gt; getAllApplicants(){
  
          EntityManager entityManager = factory.createEntityManager();
          @SuppressWarnings("unchecked")
          List&lt;Applicant&gt; applicants = entityManager.createQuery("Select a from Applicant a LEFT JOIN FETCH a.schools WHERE a.active = :active ")
          .setParameter("active", true).setHint("eclipselink.refresh", "true")
          .getResultList();
          //List&lt;Applicant&gt; applicants =  Applicant.findAllApplicants();
          System.out.println("The long query getting applicants");
          entityManager.close();
          return applicants;  
      }
  </code></pre>
  
  <p>If i try to change my query to use nested joining, i get an error saying relationship not recognized. I tried to query like so:</p>
  
  <pre><code>entityManager.createQuery("Select a from Applicant a LEFT JOIN FETCH a.schools WHERE LEFT JOIN FETCH a.person.school a.active = :active ")
  </code></pre>
  
  <p><strong>UPDATED</strong></p>
  
  <p>After removing the keyword "left join fetch" my new query:</p>
  
  <pre><code>List&lt;Applicant&gt; applicants = entityManager.createQuery("Select a from Applicant a WHERE a.active = :active ")
          .setParameter("active", true)
          .getResultList();
  </code></pre>
  
  <p>The only query i agree with that it first creates is this one</p>
  
  <pre><code>SELECT personID, TYPE, DATEADDED, FIRSTNAME, LASTNAME, MIDDLENAME, ACTIVE, BIRTHDAY, EMAILADDRESS, ETHNICITY, GENDER, HISPANIC, IMAGEPATH, MARITAL, NATIVELANGUAGE, PRIMARYTELEPHONE, RELIGIOUSAFFILIATION, SECONDARYTELEPHONE, version, addressID, schoolID, MAJOR FROM PERSON WHERE ((ACTIVE = ?) AND (TYPE = ?))
      bind =&gt; [true, AP]
  </code></pre>
  
  <p>Returns this stack trace and the following sql's selects are created:</p>
  
  <pre><code>[EL Fine]: sql: 2012-07-13 17:25:52.297--ServerSession(1529073996)--Connection(726700617)--Thread(Thread["http-bio-8080"-exec-18,5,main])--SELECT schoolID, ACTIVE, ADMISSIONSEMAILADDRESS, ADMISSIONSPHONE, CODE, description, HELPGENERALEMAILADDRESS, NAME, PRIMARYPHONE, version, addressID FROM SCHOOL WHERE (schoolID = ?)
      bind =&gt; [1]
  [EL Fine]: sql: 2012-07-13 17:25:52.305--ServerSession(1529073996)--Connection(1615948530)--Thread(Thread["http-bio-8080"-exec-18,5,main])--SELECT id, ACTIVE, CODE, DESCRIPTION, NAME, version, SCHOOLDEPARTMENT_schoolID FROM DEPARTMENT WHERE (SCHOOLDEPARTMENT_schoolID = ?)
      bind =&gt; [1]
  [EL Fine]: sql: 2012-07-13 17:25:52.308--ServerSession(1529073996)--Connection(893810654)--Thread(Thread["http-bio-8080"-exec-18,5,main])--SELECT programID, ACTIVE, CODE, description, NAME, PROGRAMTYPE, REQUIREDCREDITS, version, SCHOOL_schoolID FROM PROGRAM WHERE (SCHOOL_schoolID = ?)
      bind =&gt; [1]
  [EL Fine]: sql: 2012-07-13 17:25:52.31--ServerSession(1529073996)--Connection(399107363)--Thread(Thread["http-bio-8080"-exec-18,5,main])--SELECT personID, TYPE, DATEADDED, FIRSTNAME, LASTNAME, MIDDLENAME, ACTIVE, BIRTHDAY, EMAILADDRESS, ETHNICITY, GENDER, HISPANIC, IMAGEPATH, MARITAL, NATIVELANGUAGE, PRIMARYTELEPHONE, RELIGIOUSAFFILIATION, SECONDARYTELEPHONE, version, addressID, schoolID, MAJOR, studentId FROM PERSON WHERE (schoolID = ?)
      bind =&gt; [1]
  [EL Fine]: sql: 2012-07-13 17:25:52.314--ServerSession(1529073996)--Connection(464642021)--Thread(Thread["http-bio-8080"-exec-18,5,main])--SELECT registrationId, SEMESTERTYPE, version, YEAR, semesterCourseId, personID FROM REGISTRATION WHERE (personID = ?)
      bind =&gt; [501]
  [EL Fine]: sql: 2012-07-13 17:25:52.322--ServerSession(1529073996)--Connection(395757277)--Thread(Thread["http-bio-8080"-exec-18,5,main])--SELECT registrationId, SEMESTERTYPE, version, YEAR, semesterCourseId, personID FROM REGISTRATION WHERE (personID = ?)
      bind =&gt; [552]
  [EL Fine]: sql: 2012-07-13 17:25:52.327--ServerSession(1529073996)--Connection(328677993)--Thread(Thread["http-bio-8080"-exec-18,5,main])--SELECT registrationId, SEMESTERTYPE, version, YEAR, semesterCourseId, personID FROM REGISTRATION WHERE (personID = ?)
      bind =&gt; [602]
  [EL Fine]: sql: 2012-07-13 17:25:52.358--ServerSession(1529073996)--Connection(873312397)--Thread(Thread["http-bio-8080"-exec-18,5,main])--SELECT addressID, CITY, COUNTRY, STATE_US, STREETADDRESS, STREETADDRESS2, version, ZIPCODE FROM ADDRESS WHERE (addressID = ?)
      bind =&gt; [2]
  [EL Fine]: sql: 2012-07-13 17:25:52.365--ServerSession(1529073996)--Connection(509650638)--Thread(Thread["http-bio-8080"-exec-18,5,main])--SELECT applicantSchoolID, CREDITSCOMPLETED, FROMMONTH, FROMYEAR, GPA, NAME, SCHOOLTYPE, TOMONTH, TOYEAR, version, APPLICANT_personID FROM APPLICANTSCHOOL WHERE (APPLICANT_personID = ?)
      bind =&gt; [151]
  [EL Fine]: sql: 2012-07-13 17:25:52.385--ServerSession(1529073996)--Connection(17334753)--Thread(Thread["http-bio-8080"-exec-18,5,main])--SELECT applicantSchoolID, CREDITSCOMPLETED, FROMMONTH, FROMYEAR, GPA, NAME, SCHOOLTYPE, TOMONTH, TOYEAR, version, APPLICANT_personID FROM APPLICANTSCHOOL WHERE (APPLICANT_personID = ?)
      bind =&gt; [51]
  </code></pre>