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

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

Introduction

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

Prototype

public CriteriaQueryTranslator(final SessionFactoryImplementor factory, final CriteriaImpl criteria,
            final String rootEntityName, final String rootSQLAlias) throws HibernateException 

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:corner.orm.tapestry.table.RelativePersistentBasicTableModel.java

License:Apache License

private Query createQuery(Session session, Collection c, String selectStr, String orderStr) {
    //TODO ?/*from   www  .j  a va2  s .c  o  m*/

    StringBuffer sb = new StringBuffer();
    if (selectStr != null) {
        sb.append(selectStr).append(" ");
    }

    Query query;
    if (callback != null) {
        //========  Example?.
        Criteria criteria = callback.createCriteria(session);

        //?
        callback.appendCriteria(criteria);
        String rootEntityName = ((CriteriaImpl) criteria).getEntityOrClassName();
        CriteriaQueryTranslator criteriaQuery = new CriteriaQueryTranslator(
                (SessionFactoryImplementor) session.getSessionFactory(), (CriteriaImpl) criteria,
                rootEntityName, Criteria.ROOT_ALIAS);
        String where = criteriaQuery.getWhereCondition();
        QueryParameters qps = criteriaQuery.getQueryParameters();

        //Criteria??where??
        if (where != null && where.length() > 0) {
            sb.append("where ").append(where).append(" ");
        }
        if (orderStr != null) {
            sb.append(orderStr);
        }
        query = session.createFilter(c, sb.toString()).setParameters(qps.getPositionalParameterValues(),
                qps.getPositionalParameterTypes());
    } else {
        if (orderStr != null) {
            sb.append(orderStr);
        }
        query = session.createFilter(c, sb.toString());

    }

    return query;

}

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

License:Open Source License

/**
 * {@inheritDoc}/* www.j a  va  2s .  co m*/
 */
@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 ww  w .  j  av a2  s . c om

    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 ww w . j  a va  2  s.c o m
 *            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 a v a2 s.c om
}