title: How do a Criteria polymorphic join in JPA?
body: <p>I have a project that have a modeling like this:</p>
  
  <pre><code>public abstract class Actor{}
  public class Person extends Actor {}
  public class Organization extends Actor {}
  
  public abstract class Role{ @ManyToOne @JoinColumn(name="ID_ACTOR") }
  public class Customer extends Role{}
  public class Employee extends Role{}
  </code></pre>
  
  <p>I would like to get a <code>List&lt;Role&gt;</code> which plays a particular Actor:</p>
  
  <pre><code>public List&lt;Role&gt; getRoles(Actor actor) {
    CriteriaBuilder builder = em.getCriteriaBuilder();
    CriteriaQuery&lt;Role&gt; criteria = builder.createQuery(Role.class);
  
    //How to do this filter using Criteria API (JPA 2.1)?
  
    return query.getResultList();
  }
  </code></pre>
  
  <p>The SQL below makes the job:</p>
  
  
  
  <pre class="lang-sql prettyprint-override"><code>SELECT 'Customer' as role, c.id as id, c.id_actor as actor
  FROM customer c
  left join person p on p.id = c.id_actor
  left join organization o on o.id = c.id_actor
  where c.id_actor = ?
  
  UNION 
  
  SELECT 'Employee' as role, e.id as id, e.id_actor as actor
  FROM employee e
  left join person p on p.id = e.id_actor
  left join organization o on o.id = e.id_actor
  where e.id_actor = ?
  </code></pre>