Search in sources :

Example 1 with SqmFunction

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();
}
Also used : QuerySqmImpl(org.hibernate.query.sqm.internal.QuerySqmImpl) SqmSelectStatement(org.hibernate.query.sqm.tree.select.SqmSelectStatement) Transaction(org.hibernate.Transaction) SqmSelection(org.hibernate.query.sqm.tree.select.SqmSelection) SqmFunction(org.hibernate.query.sqm.tree.expression.SqmFunction) Session(org.hibernate.Session) Test(org.junit.Test)

Aggregations

Session (org.hibernate.Session)1 Transaction (org.hibernate.Transaction)1 QuerySqmImpl (org.hibernate.query.sqm.internal.QuerySqmImpl)1 SqmFunction (org.hibernate.query.sqm.tree.expression.SqmFunction)1 SqmSelectStatement (org.hibernate.query.sqm.tree.select.SqmSelectStatement)1 SqmSelection (org.hibernate.query.sqm.tree.select.SqmSelection)1 Test (org.junit.Test)1