title: Hibernate, how to create an alias on a property of a subclass
body: <p>I have the following class hierarchy/hibernate mapping:</p>
  
  <pre><code>&lt;class name="Document"
         table="KP_DOCUMENT"&gt;
      ...
  &lt;/class&gt;
  
  &lt;subclass name="CurrentAccountDocument"
            extends="Document"
            lazy="false"&gt;
      &lt;many-to-one name="bankAccount"
                   class="CurrentAccount"
                   column="IBAN"
                   access="field"
                   not-null="true"
                   lazy="false"
                   cascade="none"/&gt;
  &lt;/subclass&gt;
  
  &lt;class name="CurrentAccount"
         table="KP_CURRENTACCOUNT"&gt;
      &lt;id name="iban"
          column="IBAN"
          type="string"
          access="field"/&gt;
      &lt;property name="baseNumber"
                column="BASENR"
                type="string"
                access="property"/&gt;
      ...
  &lt;/class&gt;
  </code></pre>
  
  <p>I need to include properties of the class <em>BankAccount</em> as restrictions in a query to select objects of the class <em>Document</em>. So I need to create an alias for the many-to-one property <em>bankAccount</em> in the class <em>CurrentAccountDocument</em>, which is a subclass of <em>Document</em>. So when I try the following:</p>
  
  <pre><code>Criteria criteria = session.createCriteria(Document.class);
  criteria.createAlias("bankAccount", "bankAccount");
  queryCriterion = Restrictions.eq("bankAccount.baseNumber", "35300");
  criteria.add(queryCriterion);
  return criteria.list();
  </code></pre>
  
  <p>... it results in the following exception:</p>
  
  <pre><code>org.hibernate.QueryException: could not resolve property: bankAccount of: Document
  </code></pre>
  
  <p>I can access "normal" subclass properties in other restrictions. However with createAlias() this doesn't work. How is this done correctly?</p>
  
  <p>BTW, I tried the same with the JPA criteriy API like this:</p>
  
  <pre><code>CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder();
  CriteriaQuery&lt;Document&gt; criteriaQuery = criteriaBuilder.createQuery(Document.class);
  Query&lt;Document&gt; documentRoot = criteriaQuery.from(Document.class);
  Join joinBankAccount = documentRoot.join("bankAccount");
  </code></pre>
  
  <p>or alternatively</p>
  
  <pre><code>Join joinBankAccount = criteriaBuilder.treat(documentRoot, CurrentAccountDocument.class).join("bankAccount");
  Predicate queryPredicate = criteriaBuilder.equal(joinBankAccount.get("baseNumber"), "4711");
  criteriaQuery.select(documentRoot).where(queryPredicate);
  query = session.createQuery(criteriaQuery);
  return query.getResultList();
  </code></pre>
  
  <p>... which results in a similar exception:</p>
  
  <pre><code>Caused by: org.hibernate.QueryException: could not resolve property: bankAccount of: Document [select generatedAlias0 from Document as generatedAlias0 inner join generatedAlias0.documentContent as generatedAlias1 inner join generatedAlias0.bankAccount as generatedAlias2 where generatedAlias2.baseNumber=:param0 order by generatedAlias1.creationTimeStamp desc]
  at org.hibernate.QueryException.generateQueryException(QueryException.java:120)
  at org.hibernate.QueryException.wrapWithQueryString(QueryException.java:103)
  at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:220)
  at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:144)
  at org.hibernate.engine.query.spi.HQLQueryPlan.&lt;init&gt;(HQLQueryPlan.java:113)
  at org.hibernate.engine.query.spi.HQLQueryPlan.&lt;init&gt;(HQLQueryPlan.java:73)
  at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:158)
  at org.hibernate.internal.AbstractSharedSessionContract.getQueryPlan(AbstractSharedSessionContract.java:611)
  at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:720)
  </code></pre>
  
  <p>Due to other probles with the JPA API I switched back to the deprecated hibernate criteria API until JPA works correctly. So please tell me how it works with hibernate. If you have a solution with JPA it is also welcome.</p>
  
  <p>Thank you Thomas</p>