use of org.hibernate.query.sqm.tree.expression.SqmFunction in project hibernate-orm by hibernate.
the class ASTParserLoadingTest method testAliases.
@Test
public void testAliases() {
Session s = openSession();
Transaction t = s.beginTransaction();
Animal a = new Animal();
a.setBodyWeight(12.4f);
a.setDescription("an animal");
s.persist(a);
Query<?> q = s.createQuery("select a.bodyWeight as abw, a.description from Animal a");
SqmSelectStatement<?> sqmStatement = (SqmSelectStatement<?>) q.unwrap(QuerySqmImpl.class).getSqmStatement();
List<SqmSelection<?>> selections = sqmStatement.getQuerySpec().getSelectClause().getSelections();
assertThat(selections.size(), is(2));
assertThat(selections.get(0).getAlias(), is("abw"));
assertThat(selections.get(1).getAlias(), nullValue());
q = s.createQuery("select count(*), avg(a.bodyWeight) as avg from Animal a");
sqmStatement = (SqmSelectStatement<?>) q.unwrap(QuerySqmImpl.class).getSqmStatement();
selections = sqmStatement.getQuerySpec().getSelectClause().getSelections();
assertThat(selections.size(), is(2));
assertThat(selections.get(0), notNullValue());
assertThat(selections.get(0).getAlias(), nullValue());
assertThat(selections.get(0).getSelectableNode(), instanceOf(SqmFunction.class));
assertThat(((SqmFunction) selections.get(0).getSelectableNode()).getFunctionName(), is("count"));
assertThat(selections.get(1), notNullValue());
assertThat(selections.get(1).getAlias(), notNullValue());
assertThat(selections.get(1).getAlias(), is("avg"));
assertThat(selections.get(1).getSelectableNode(), instanceOf(SqmFunction.class));
assertThat(((SqmFunction) selections.get(1).getSelectableNode()).getFunctionName(), is("avg"));
s.delete(a);
t.commit();
s.close();
}
Aggregations