title: JPQL equivalent of SQL query using unions and selecting constants
body: <p>I've written a SQL query that basically selects from a number of tables to determine which ones have rows that were created since a particular date.  My SQL looks something like this:</p>
  
  <pre><code>SELECT widget_type FROM(
    SELECT 'A' as widget_type
    FROM widget_a
    WHERE creation_timestamp &gt; :cutoff
    UNION
    SELECT 'B' as widget_type
    FROM widget_b
    WHERE creation_timestamp &gt; :cutoff
  ) types
  GROUP BY widget_type
  HAVING count(*)&gt;0
  </code></pre>
  
  <p>That works well in SQL but I recently found that, while JPA may use unions to perform "table per class" polymorphic queries, <a href="https://stackoverflow.com/questions/15062501/jpa-only-executes-first-select-statement-in-union">JPQL does not support unions</a> in queries.  So that leaves me wondering whether JPA has an alternative I could use to accomplish the same thing.</p>
  
  <p>In reality, I would be querying against a dozen tables, not just two, so I would like to avoid doing separate queries.  I would also like to avoid doing a native SQL query for portability reasons.</p>
  
  <p>In the question I linked to above, it was asked whether the entities that map to widget_a and widget_b are part of the same inheritance tree.  Yes, they are.  However, if I selected from their base class, I don't believe I would have a way of specifying different string constants for the different child entities, would I?  If I could select an entity's class name instead of a string I provide, that might serve my purpose too.  But I don't know if that's possible either.  Thoughts?</p>