Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Having done some googling, it appears that this this is not possible - Hibernate only allows inclusion of the root entity alias using <code>{alias}</code> in the SQL string of the <code>SQLProjection</code>. I did however find <a href="https://hibernate.atlassian.net/browse/HHH-2952" rel="nofollow noreferrer">this issue regarding the limitation</a> on the Hibernate JIRA pages.</p> <p>Someone has kindly submitted a patch that allows the use of non-root aliases in the <code>SQLProjection</code> string, through a new <code>RestrictionsExt</code> class. Using my example from the question:</p> <pre><code>Projections.sqlProjection("MIN(i.powerRestarts) as value", new String[]{"value"}, new Type[]{new LongType()})) </code></pre> <p>The alias <code>i</code> can now be referenced as:</p> <pre><code>RestrictionsExt.sqlProjection("MIN({i}.powerRestarts) as value", "value", new LongType()) </code></pre> <p>I had to modify the static <code>RestrictionsExt.sqlProjection</code> method to allow specification of the type for the column alias (<code>"value"</code>) (here defined as <code>LongType</code>), as the patch didn't allow this and defaulted to <code>StringType</code>.</p> <p>The SQLAliasedProjection class in the patch also requires access to the following private methods in <code>org.hibernate.loader.criteria.CriteriaQueryTranslator</code>: <code>getOuterQueryTranslator</code> and <code>getAliasedCriteria</code>. To get this to work without modifying the Hibernate source, I used reflection:</p> <pre><code>cri = ((org.hibernate.loader.criteria.CriteriaQueryTranslator) criteriaQuery).getAliasedCriteria(alias); </code></pre> <p>was changed to:</p> <pre><code>Method m = ((org.hibernate.loader.criteria.CriteriaQueryTranslator) criteriaQuery).getClass().getDeclaredMethod("getAliasedCriteria", String.class); m.setAccessible(true); cri = (Criteria) m.invoke(((org.hibernate.loader.criteria.CriteriaQueryTranslator) criteriaQuery), alias); </code></pre> <p>Hopefully this will help anyone else facing the same problem.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload