title: jpa native query retrieve multiple entities
body: <p>I have a database with 4 tables:</p>
  
  <pre><code>company,staff,department,project
  </code></pre>
  
  <p><strong>Company.java</strong><br /></p>
  
  <pre><code>@Entity
  @Table(name = "company")
  @SqlResultSetMapping(name = "COMPANY", entities = 
  {
      @EntityResult(entityClass = Company.class),
      @EntityResult(entityClass = Staff.class)
  })
  ...
  </code></pre>
  
  <p><strong>GetEntity.java</strong><br /></p>
  
  <pre><code>EntityManagerFactory emf = Persistence.createEntityManagerFactory("GetEntityPU");
  EntityManager em = emf.createEntityManager();
  
  String query = "SELECT * 
                  FROM company c 
                  JOIN staff s 
                  ON c.ID = s.companyID";
  Query q = em.createNativeQuery(query, "COMPANY");
  List&lt;Object[]&gt; list = q.getResultList();
  </code></pre>
  
  <p>From above code, I can retrieve all data from Company entity and Staff entity. <br /></p>
  
  <p>Now I want to retrieve all data from any 2 tables:<br />
  maybe all data for company, staff tables <strong>OR</strong> all data for staff, department tables</p>
  
  <p>How should I control every entity in my query? <br />
  I really no ideas on how to do it.<br />
  Any ideas or useful source link are welcome. </p>