Search in sources :

Example 16 with QueryTranslator

use of org.hibernate.hql.spi.QueryTranslator in project jbosstools-hibernate by jbosstools.

the class FacadeFactoryTest method testCreateQueryTranslator.

@Test
public void testCreateQueryTranslator() {
    QueryTranslator queryTranslator = (QueryTranslator) Proxy.newProxyInstance(facadeFactory.getClassLoader(), new Class[] { QueryTranslator.class }, new TestInvocationHandler());
    IQueryTranslator facade = facadeFactory.createQueryTranslator(queryTranslator);
    Assert.assertSame(queryTranslator, ((IFacade) facade).getTarget());
}
Also used : PersistentClass(org.hibernate.mapping.PersistentClass) IPOJOClass(org.jboss.tools.hibernate.runtime.spi.IPOJOClass) RootClass(org.hibernate.mapping.RootClass) IPersistentClass(org.jboss.tools.hibernate.runtime.spi.IPersistentClass) POJOClass(org.hibernate.tool.hbm2x.pojo.POJOClass) IQueryTranslator(org.jboss.tools.hibernate.runtime.spi.IQueryTranslator) QueryTranslator(org.hibernate.hql.spi.QueryTranslator) IQueryTranslator(org.jboss.tools.hibernate.runtime.spi.IQueryTranslator) Test(org.junit.Test)

Example 17 with QueryTranslator

use of org.hibernate.hql.spi.QueryTranslator in project jbosstools-hibernate by jbosstools.

the class FacadeFactoryTest method testCreateQueryTranslator.

@Test
public void testCreateQueryTranslator() {
    QueryTranslator queryTranslator = (QueryTranslator) Proxy.newProxyInstance(facadeFactory.getClassLoader(), new Class[] { QueryTranslator.class }, new TestInvocationHandler());
    IQueryTranslator facade = facadeFactory.createQueryTranslator(queryTranslator);
    Assert.assertSame(queryTranslator, ((IFacade) facade).getTarget());
}
Also used : PersistentClass(org.hibernate.mapping.PersistentClass) IPOJOClass(org.jboss.tools.hibernate.runtime.spi.IPOJOClass) RootClass(org.hibernate.mapping.RootClass) IPersistentClass(org.jboss.tools.hibernate.runtime.spi.IPersistentClass) POJOClass(org.hibernate.tool.hbm2x.pojo.POJOClass) IQueryTranslator(org.jboss.tools.hibernate.runtime.spi.IQueryTranslator) QueryTranslator(org.hibernate.hql.spi.QueryTranslator) IQueryTranslator(org.jboss.tools.hibernate.runtime.spi.IQueryTranslator) Test(org.junit.Test)

Example 18 with QueryTranslator

use of org.hibernate.hql.spi.QueryTranslator in project midpoint by Evolveum.

the class HibernateToSqlTranslator method toSql.

/**
 * Do not use in production code! Only for testing purposes only. Used for example during query engine upgrade.
 * Method provides translation from hibernate HQL query to plain SQL string query.
 *
 * @param sessionFactory
 * @param hqlQueryText
 * @return SQL string, null if hqlQueryText parameter is empty.
 */
public static String toSql(SessionFactory sessionFactory, String hqlQueryText) {
    Validate.notNull(sessionFactory, "Session factory must not be null.");
    if (StringUtils.isEmpty(hqlQueryText)) {
        return null;
    }
    final QueryTranslatorFactory translatorFactory = new ASTQueryTranslatorFactory();
    final SessionFactoryImplementor factory = (SessionFactoryImplementor) sessionFactory;
    final QueryTranslator translator = translatorFactory.createQueryTranslator(hqlQueryText, hqlQueryText, Collections.EMPTY_MAP, factory, null);
    translator.compile(Collections.EMPTY_MAP, false);
    return translator.getSQLString();
}
Also used : QueryTranslatorFactory(org.hibernate.hql.spi.QueryTranslatorFactory) ASTQueryTranslatorFactory(org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) QueryTranslator(org.hibernate.hql.spi.QueryTranslator) ASTQueryTranslatorFactory(org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory)

Example 19 with QueryTranslator

use of org.hibernate.hql.spi.QueryTranslator in project hibernate-orm by hibernate.

the class HQLQueryPlan method performList.

/**
 * Coordinates the efforts to perform a list across all the included query translators.
 *
 * @param queryParameters The query parameters
 * @param session The session
 *
 * @return The query result list
 *
 * @throws HibernateException Indicates a problem performing the query
 */
@SuppressWarnings("unchecked")
public List performList(QueryParameters queryParameters, SharedSessionContractImplementor session) throws HibernateException {
    if (traceEnabled) {
        LOG.tracev("Find: {0}", getSourceQuery());
        queryParameters.traceParameters(session.getFactory());
    }
    final RowSelection rowSelection = queryParameters.getRowSelection();
    final boolean hasLimit = rowSelection != null && rowSelection.definesLimits();
    final boolean needsLimit = hasLimit && translators.length > 1;
    final QueryParameters queryParametersToUse;
    if (needsLimit) {
        LOG.needsLimit();
        final RowSelection selection = new RowSelection();
        selection.setFetchSize(queryParameters.getRowSelection().getFetchSize());
        selection.setTimeout(queryParameters.getRowSelection().getTimeout());
        queryParametersToUse = queryParameters.createCopyUsing(selection);
    } else {
        queryParametersToUse = queryParameters;
    }
    // fast path to avoid unnecessary allocation and copying
    if (translators.length == 1) {
        return translators[0].list(session, queryParametersToUse);
    }
    final int guessedResultSize = guessResultSize(rowSelection);
    final List combinedResults = new ArrayList(guessedResultSize);
    final IdentitySet distinction;
    if (needsLimit) {
        distinction = new IdentitySet(guessedResultSize);
    } else {
        distinction = null;
    }
    int includedCount = -1;
    translator_loop: for (QueryTranslator translator : translators) {
        final List tmp = translator.list(session, queryParametersToUse);
        if (needsLimit) {
            // NOTE : firstRow is zero-based
            final int first = queryParameters.getRowSelection().getFirstRow() == null ? 0 : queryParameters.getRowSelection().getFirstRow();
            final int max = queryParameters.getRowSelection().getMaxRows() == null ? -1 : queryParameters.getRowSelection().getMaxRows();
            for (final Object result : tmp) {
                if (!distinction.add(result)) {
                    continue;
                }
                includedCount++;
                if (includedCount < first) {
                    continue;
                }
                combinedResults.add(result);
                if (max >= 0 && includedCount > max) {
                    // break the outer loop !!!
                    break translator_loop;
                }
            }
        } else {
            combinedResults.addAll(tmp);
        }
    }
    return combinedResults;
}
Also used : IdentitySet(org.hibernate.internal.util.collections.IdentitySet) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) QueryParameters(org.hibernate.engine.spi.QueryParameters) RowSelection(org.hibernate.engine.spi.RowSelection) QueryTranslator(org.hibernate.hql.spi.QueryTranslator)

Example 20 with QueryTranslator

use of org.hibernate.hql.spi.QueryTranslator in project hibernate-orm by hibernate.

the class CriteriaHQLAlignmentTest method testHQLAggregationReturnType.

@Test
public void testHQLAggregationReturnType() {
    // EJB3: COUNT returns Long
    QueryTranslatorImpl translator = createNewQueryTranslator("select count(*) from Human h");
    assertEquals("incorrect return type count", 1, translator.getReturnTypes().length);
    assertEquals("incorrect return type", LongType.INSTANCE, translator.getReturnTypes()[0]);
    translator = createNewQueryTranslator("select count(h.heightInches) from Human h");
    assertEquals("incorrect return type count", 1, translator.getReturnTypes().length);
    assertEquals("incorrect return type", LongType.INSTANCE, translator.getReturnTypes()[0]);
    // MAX, MIN return the type of the state-field to which they are applied.
    translator = createNewQueryTranslator("select max(h.heightInches) from Human h");
    assertEquals("incorrect return type count", 1, translator.getReturnTypes().length);
    assertEquals("incorrect return type", DoubleType.INSTANCE, translator.getReturnTypes()[0]);
    translator = createNewQueryTranslator("select max(h.id) from Human h");
    assertEquals("incorrect return type count", 1, translator.getReturnTypes().length);
    assertEquals("incorrect return type", LongType.INSTANCE, translator.getReturnTypes()[0]);
    // AVG returns Double.
    translator = createNewQueryTranslator("select avg(h.heightInches) from Human h");
    assertEquals("incorrect return type count", 1, translator.getReturnTypes().length);
    assertEquals("incorrect return type", DoubleType.INSTANCE, translator.getReturnTypes()[0]);
    translator = createNewQueryTranslator("select avg(h.id) from Human h");
    assertEquals("incorrect return type count", 1, translator.getReturnTypes().length);
    assertEquals("incorrect return type", DoubleType.INSTANCE, translator.getReturnTypes()[0]);
    translator = createNewQueryTranslator("select avg(h.bigIntegerValue) from Human h");
    assertEquals("incorrect return type count", 1, translator.getReturnTypes().length);
    assertEquals("incorrect return type", DoubleType.INSTANCE, translator.getReturnTypes()[0]);
    // SUM returns Long when applied to state-fields of integral types (other than BigInteger);
    translator = createNewQueryTranslator("select sum(h.id) from Human h");
    assertEquals("incorrect return type count", 1, translator.getReturnTypes().length);
    assertEquals("incorrect return type", LongType.INSTANCE, translator.getReturnTypes()[0]);
    translator = createNewQueryTranslator("select sum(h.intValue) from Human h");
    assertEquals("incorrect return type count", 1, translator.getReturnTypes().length);
    assertEquals("incorrect return type", LongType.INSTANCE, translator.getReturnTypes()[0]);
    // SUM returns Double when applied to state-fields of floating point types;
    translator = createNewQueryTranslator("select sum(h.heightInches) from Human h");
    assertEquals("incorrect return type count", 1, translator.getReturnTypes().length);
    assertEquals("incorrect return type", DoubleType.INSTANCE, translator.getReturnTypes()[0]);
    translator = createNewQueryTranslator("select sum(h.floatValue) from Human h");
    assertEquals("incorrect return type count", 1, translator.getReturnTypes().length);
    assertEquals("incorrect return type", DoubleType.INSTANCE, translator.getReturnTypes()[0]);
    // SUM returns BigInteger when applied to state-fields of type BigInteger
    translator = createNewQueryTranslator("select sum(h.bigIntegerValue) from Human h");
    assertEquals("incorrect return type count", 1, translator.getReturnTypes().length);
    assertEquals("incorrect return type", BigIntegerType.INSTANCE, translator.getReturnTypes()[0]);
    // SUM and BigDecimal when applied to state-fields of type BigDecimal.
    translator = createNewQueryTranslator("select sum(h.bigDecimalValue) from Human h");
    assertEquals("incorrect return type count", 1, translator.getReturnTypes().length);
    assertEquals("incorrect return type", BigDecimalType.INSTANCE, translator.getReturnTypes()[0]);
    // special case to test classicquery special case handling of count(*)
    String hql = "select count(*) from Human h";
    QueryTranslatorFactory classic = new ClassicQueryTranslatorFactory();
    QueryTranslator oldQueryTranslator = classic.createQueryTranslator(hql, hql, Collections.EMPTY_MAP, sessionFactory(), null);
    oldQueryTranslator.compile(Collections.EMPTY_MAP, true);
    assertEquals("incorrect return type count", 1, oldQueryTranslator.getReturnTypes().length);
    assertEquals("incorrect return type", LongType.INSTANCE, oldQueryTranslator.getReturnTypes()[0]);
}
Also used : ClassicQueryTranslatorFactory(org.hibernate.hql.internal.classic.ClassicQueryTranslatorFactory) QueryTranslatorFactory(org.hibernate.hql.spi.QueryTranslatorFactory) ClassicQueryTranslatorFactory(org.hibernate.hql.internal.classic.ClassicQueryTranslatorFactory) QueryTranslator(org.hibernate.hql.spi.QueryTranslator) QueryTranslatorImpl(org.hibernate.hql.internal.ast.QueryTranslatorImpl) Test(org.junit.Test)

Aggregations

QueryTranslator (org.hibernate.hql.spi.QueryTranslator)21 Test (org.junit.Test)11 QueryTranslatorFactory (org.hibernate.hql.spi.QueryTranslatorFactory)9 ASTQueryTranslatorFactory (org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory)8 PersistentClass (org.hibernate.mapping.PersistentClass)6 RootClass (org.hibernate.mapping.RootClass)6 POJOClass (org.hibernate.tool.hbm2x.pojo.POJOClass)6 IPOJOClass (org.jboss.tools.hibernate.runtime.spi.IPOJOClass)6 IPersistentClass (org.jboss.tools.hibernate.runtime.spi.IPersistentClass)6 IQueryTranslator (org.jboss.tools.hibernate.runtime.spi.IQueryTranslator)6 HQLQueryPlan (org.hibernate.engine.query.spi.HQLQueryPlan)4 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)4 HashMap (java.util.HashMap)3 ClassicQueryTranslatorFactory (org.hibernate.hql.internal.classic.ClassicQueryTranslatorFactory)3 Map (java.util.Map)2 MappingException (org.hibernate.MappingException)2 QueryException (org.hibernate.QueryException)2 QueryTranslatorImpl (org.hibernate.hql.internal.ast.QueryTranslatorImpl)2 TestForIssue (org.hibernate.testing.TestForIssue)2 ArrayList (java.util.ArrayList)1