title: Is it possible to get the SQL alias of a join table for a Hibernate sqlRestriction?
body: <p>I have a Person class which has a String collection of aliases representing additional names that person may go by.  For example, Clark Kent may have aliases "Superman" and "Man of Steel".  Dwight Howard also has an alias of "Superman".</p>
  
  <pre><code>@Entity
  class Person {
  
    @CollectionOfElements(fetch=FetchType.EAGER)
    Set&lt;String&gt; aliases = new TreeSet&lt;String&gt;();
  </code></pre>
  
  <p>Hibernate creates two tables in my database, Person and Person_aliases.  Person_aliases is a join table with the columns Person_id and element.  Let's say Person_aliases has the following data</p>
  
  <pre><code>--------------------------------
  | Person_id     | element      |
  --------------------------------
  | Clark Kent    | Superman     |
  | Clark Kent    | Man of Steel |
  | Dwight Howard | Superman     |
  | Bruce Wayne   | Batman       |
  --------------------------------
  </code></pre>
  
  <p>I want to make a hibernate Criteria query for all persons who go by the alias of "Superman".  </p>
  
  <p>For reasons too long to list here, I'd really like to make this a Criteria query, not an HQL query (unless it's possible to add an HQL restriction on a Criteria object, in which case I'm all ears) or a raw SQL query.  Since according to <a href="https://stackoverflow.com/questions/2321214/how-do-i-query-for-objects-with-a-value-in-a-string-collection-using-hibernate-cr">How do I query for objects with a value in a String collection using Hibernate Criteria?</a> it is impossible to refer to elements of value-type collections using the CriteriaAPI I thought I'd resort to adding an SqlRestriction on my criteria object.</p>
  
  <pre><code>Criteria crit = session.createCriteria(Person.class);
  crit.add(Restrictions.sqlRestriction("XXXXX.element='superman'");
  </code></pre>
  
  <p>in the hopes that Hibernate will create an SQL statement like</p>
  
  <pre><code>    select *
  from
      Person this_ 
  left outer join
      Person_aliases aliases2_ 
          on this_.id=aliases2_.Person_id 
  where
      XXXXX.element='superman' 
  </code></pre>
  
  <p>However, I need to fill in the XXXXX with the table alias for the Person_aliases table in the SQL query, which in this case would be 'aliases2_'. I noticed that if I needed the reference to the Person table alias I could use {alias}.  But this won't work because Person is the primary table for this Criteria, not Person_aliases.</p>
  
  <p>What do I fill in for the XXXXX?  If there is no nice substition token like {alias} then is there a way I could get hibernate to tell me what that alias is going to be? I noticed a method called generateAlias() org.hibernate.util.StringHelper class.  Would this help me predict what the alias would be?  </p>
  
  <p>I'd really, really like to avoid hard coding 'aliases2_'.  </p>
  
  <p>Thanks for your time!</p>