Example usage for org.hibernate.loader.criteria CriteriaQueryTranslator ROOT_SQL_ALIAS

List of usage examples for org.hibernate.loader.criteria CriteriaQueryTranslator ROOT_SQL_ALIAS

Introduction

In this page you can find the example usage for org.hibernate.loader.criteria CriteriaQueryTranslator ROOT_SQL_ALIAS.

Prototype

String ROOT_SQL_ALIAS

To view the source code for org.hibernate.loader.criteria CriteriaQueryTranslator ROOT_SQL_ALIAS.

Click Source Link

Usage

From source file:com.qcadoo.model.internal.HibernateServiceImpl.java

License:Open Source License

@Override
public int getTotalNumberOfEntities(final Criteria criteria) {
    final CriteriaImpl criteriaImpl = (CriteriaImpl) criteria;
    final SessionImplementor session = (SessionImplementor) getCurrentSession();
    SessionFactoryImplementor factory = session.getFactory();
    CriteriaQueryTranslator translator = new CriteriaQueryTranslator(factory, criteriaImpl,
            criteriaImpl.getEntityOrClassName(), CriteriaQueryTranslator.ROOT_SQL_ALIAS);
    String[] implementors = factory.getImplementors(criteriaImpl.getEntityOrClassName());

    CriteriaJoinWalker walker = new CriteriaJoinWalker(
            (OuterJoinLoadable) factory.getEntityPersister(implementors[0]), translator, factory, criteriaImpl,
            criteriaImpl.getEntityOrClassName(), session.getLoadQueryInfluencers());

    final String sql = "select count(*) as cnt from (" + walker.getSQLString() + ") sq";

    getCurrentSession().flush(); // is this safe?

    return ((Number) getCurrentSession().createSQLQuery(sql)
            .setParameters(translator.getQueryParameters().getPositionalParameterValues(),
                    translator.getQueryParameters().getPositionalParameterTypes())
            .uniqueResult()).intValue();
}

From source file:com.qcadoo.model.internal.HibernateServiceImpl.java

License:Open Source License

@Override
public InternalDataDefinition resolveDataDefinition(final Criteria criteria) {
    final CriteriaImpl criteriaImpl = (CriteriaImpl) criteria;
    final SessionImplementor session = (SessionImplementor) getCurrentSession();
    SessionFactoryImplementor factory = session.getFactory();
    CriteriaQueryTranslator translator = new CriteriaQueryTranslator(factory, criteriaImpl,
            criteriaImpl.getEntityOrClassName(), CriteriaQueryTranslator.ROOT_SQL_ALIAS);

    String[] aliases = criteriaImpl.getProjection().getAliases();
    Type[] types = criteriaImpl.getProjection().getTypes(criteriaImpl, translator);

    return resolveDataDefinition(types, aliases);
}

From source file:org.candlepin.model.DetachedCandlepinQuery.java

License:Open Source License

/**
 * {@inheritDoc}//w w  w  .ja  v  a  2  s . com
 */
@Override
@SuppressWarnings({ "unchecked", "checkstyle:indentation" })
public int getRowCount() {
    CriteriaImpl executable = (CriteriaImpl) this.getExecutableCriteria();

    // Impl note:
    // We're using the projection method here over using a cursor to scroll the results due to
    // limitations on various connectors' cursor implementations. Some don't properly support
    // fast-forwarding/jumping (Oracle) and others fake the whole thing by running the query
    // and pretending to scroll (MySQL). Until these are addressed, the hack below is going to
    // be far more performant and significantly safer (which makes me sad).

    // Remove any ordering that may be applied (since we almost certainly won't have the field
    // available anymore)
    for (Iterator iterator = executable.iterateOrderings(); iterator.hasNext();) {
        iterator.next();
        iterator.remove();
    }

    Projection projection = executable.getProjection();
    if (projection != null && projection.isGrouped()) {
        // We have a projection that alters the grouping of the query. We need to rebuild the
        // projection such that it gets our row count and properly applies the group by
        // statement.
        // The logic for this block is largely derived from this Stack Overflow posting:
        // http://stackoverflow.com/
        //     questions/32498229/hibernate-row-count-on-criteria-with-already-set-projection
        //
        // A safer alternative may be to generate a query that uses the given criteria as a
        // subquery (SELECT count(*) FROM (<criteria SQL>)), but is probably less performant
        // than this hack.
        CriteriaQueryTranslator translator = new CriteriaQueryTranslator(
                (SessionFactoryImplementor) this.session.getSessionFactory(), executable,
                executable.getEntityOrClassName(), CriteriaQueryTranslator.ROOT_SQL_ALIAS);

        executable.setProjection(
                Projections.projectionList().add(Projections.rowCount()).add(Projections.sqlGroupProjection(
                        "count(count(1))", translator.getGroupBy(), new String[] {}, new Type[] {})));
    } else {
        executable.setProjection(Projections.rowCount());
    }

    Long count = (Long) executable.uniqueResult();
    return count != null ? count.intValue() : 0;
}

From source file:org.iternine.jeppetto.dao.hibernate.HibernateQueryModelDAO.java

License:Apache License

private Query createEntryBasedQuery(QueryModel queryModel) {
    Criteria criteria = getCurrentSession().createCriteria(persistentClass);

    for (String associationPath : queryModel.getAssociationConditions().keySet()) {
        criteria.createCriteria(associationPath);
    }/*from w  w w  .ja va 2s  .  c o  m*/

    CriteriaQueryTranslator criteriaQueryTranslator = new CriteriaQueryTranslator(
            (SessionFactoryImplementor) sessionFactory, (CriteriaImpl) criteria, persistentClass.getName(),
            CriteriaQueryTranslator.ROOT_SQL_ALIAS);

    StringBuilder queryStringBuilder = new StringBuilder();

    buildSelectClause(queryStringBuilder, criteriaQueryTranslator,
            queryModel.getAssociationConditions().keySet());

    List<TypedValue> parameters = buildWhereClause(queryStringBuilder, queryModel, criteria,
            criteriaQueryTranslator);

    if ((queryModel.getAssociationConditions() == null || queryModel.getAssociationConditions().isEmpty())
            && (queryModel.getSorts() == null || queryModel.getSorts().isEmpty())) {
        buildDefaultOrderClause(queryStringBuilder);
    } else {
        // can't use the default ordering by "ace.id" because of "select distinct..." syntax
        buildOrderClause(queryStringBuilder, queryModel, criteria, criteriaQueryTranslator);
    }

    Query query = getCurrentSession().createQuery(queryStringBuilder.toString());

    setParameters(parameters, query, queryModel.getAccessControlContext());

    return query;
}

From source file:org.n52.series.db.DataModelUtil.java

License:Open Source License

public static String getSqlString(Criteria criteria) {
    CriteriaImpl criteriaImpl = (CriteriaImpl) criteria;
    SessionImplementor session = criteriaImpl.getSession();
    SessionFactoryImplementor factory = extractSessionFactory(criteria);
    CriteriaQueryTranslator translator = new CriteriaQueryTranslator(factory, criteriaImpl,
            criteriaImpl.getEntityOrClassName(), CriteriaQueryTranslator.ROOT_SQL_ALIAS);
    String[] implementors = factory.getImplementors(criteriaImpl.getEntityOrClassName());

    CriteriaJoinWalker walker = new CriteriaJoinWalker(
            (OuterJoinLoadable) factory.getEntityPersister(implementors[0]), translator, factory, criteriaImpl,
            criteriaImpl.getEntityOrClassName(), session.getLoadQueryInfluencers());

    return walker.getSQLString();
}

From source file:org.n52.sos.ds.hibernate.util.HibernateHelper.java

License:Open Source License

/**
 * Get the SQL query string from Criteria.
 *
 * @param criteria/*from w  w w .  j  a v a 2s  .c om*/
 *            Criteria to get SQL query string from
 * @return SQL query string from criteria
 */
public static String getSqlString(Criteria criteria) {
    CriteriaImpl criteriaImpl = (CriteriaImpl) criteria;
    SessionImplementor session = criteriaImpl.getSession();
    SessionFactoryImplementor factory = session.getFactory();
    CriteriaQueryTranslator translator = new CriteriaQueryTranslator(factory, criteriaImpl,
            criteriaImpl.getEntityOrClassName(), CriteriaQueryTranslator.ROOT_SQL_ALIAS);
    String[] implementors = factory.getImplementors(criteriaImpl.getEntityOrClassName());

    CriteriaJoinWalker walker = new CriteriaJoinWalker(
            (OuterJoinLoadable) factory.getEntityPersister(implementors[0]), translator, factory, criteriaImpl,
            criteriaImpl.getEntityOrClassName(), session.getLoadQueryInfluencers());

    return walker.getSQLString();
}

From source file:support.SqlUtils.java

public static String getSql(Criteria criteria) {
    CriteriaImpl criteriaImpl = (CriteriaImpl) criteria;
    SessionImplementor session = criteriaImpl.getSession();
    SessionFactoryImplementor factory = session.getFactory();
    CriteriaQueryTranslator translator = new CriteriaQueryTranslator(factory, criteriaImpl,
            criteriaImpl.getEntityOrClassName(), CriteriaQueryTranslator.ROOT_SQL_ALIAS);
    String[] implementors = factory.getImplementors(criteriaImpl.getEntityOrClassName());

    CriteriaJoinWalker walker = new CriteriaJoinWalker(
            (OuterJoinLoadable) factory.getEntityPersister(implementors[0]), translator, factory, criteriaImpl,
            criteriaImpl.getEntityOrClassName(), session.getLoadQueryInfluencers());

    String sql = walker.getSQLString();
    return sql;/*from   w  w w  .j ava 2 s . co  m*/
}