Example usage for org.hibernate.criterion ProjectionList getLength

List of usage examples for org.hibernate.criterion ProjectionList getLength

Introduction

In this page you can find the example usage for org.hibernate.criterion ProjectionList getLength.

Prototype

public int getLength() 

Source Link

Usage

From source file:com.amalto.core.storage.hibernate.StandardQueryHandler.java

License:Open Source License

private ProjectionList optimizeProjectionList(ComplexTypeMetadata mainType, ProjectionList oldProjectionList) {
    ProjectionList newProjectionList = null;
    RDBMSDataSource dataSource = (RDBMSDataSource) storage.getDataSource();
    if (dataSource.getDialectName() != RDBMSDataSource.DataSourceDialect.ORACLE_10G) {
        newProjectionList = oldProjectionList;
        for (FieldMetadata keyField : mainType.getKeyFields()) {
            newProjectionList.add(Projections.groupProperty(keyField.getName()));
        }//from  ww  w .j  a v  a2s.c om
    } else { // ORACLE need to GROUP BY all selected fields
        newProjectionList = Projections.projectionList();
        Set<String> groupBys = new LinkedHashSet<String>();// GROUP BY fields
        ProjectionList extraProjectionList = Projections.projectionList();
        for (int i = 0; i < oldProjectionList.getLength(); i++) {
            String propertyName = null;
            Projection oldProjection = oldProjectionList.getProjection(i);
            if (oldProjection instanceof SQLProjection) { // Group Size
                newProjectionList.add(oldProjection);
            } else if (oldProjection instanceof PropertyProjection) {// normal fields
                propertyName = ((PropertyProjection) oldProjection).getPropertyName();
                newProjectionList.add(Projections.groupProperty(propertyName));
            } else if (oldProjection instanceof AggregateProjection) {// Max, Min
                propertyName = ((AggregateProjection) oldProjection).getPropertyName();
                newProjectionList.add(oldProjection);
                extraProjectionList.add(Projections.groupProperty(propertyName));
            }
            if (propertyName != null) {
                groupBys.add(propertyName);
            }
        }
        // Add key fields to GROUP BY
        for (FieldMetadata keyField : mainType.getKeyFields()) {
            String keyFieldName = mainType.getName() + '.' + keyField.getName();
            if (!groupBys.contains(keyFieldName)) {
                extraProjectionList.add(Projections.groupProperty(keyFieldName));
            }
        }
        for (int i = 0; i < extraProjectionList.getLength(); i++) {
            newProjectionList.add(extraProjectionList.getProjection(i));
        }
    }
    return newProjectionList;
}

From source file:com.jaspersoft.jasperserver.api.search.SearchCriteria.java

License:Open Source License

private Projection addProjectionToList(ProjectionList projectionList, Projection newProjection) {
    if (newProjection instanceof ProjectionList) {
        ProjectionList newProjectionList = ((ProjectionList) newProjection);

        for (int i = 0; i < newProjectionList.getLength(); i++) {
            projectionList.add(newProjectionList.getProjection(i));
        }/*from w  w w  .  j a  v  a  2s. c om*/
    } else {
        projectionList.add(newProjection);
    }

    return projectionList;
}

From source file:id.co.sambaltomat.core.dao.hibernate.GenericDaoHibernate.java

public int getRowCount(final List<Criterion> params, final List<JoinPath> joinPaths) {
    return (Integer) getHibernateTemplate().execute(new HibernateCallback() {
        public Object doInHibernate(Session session) throws HibernateException, SQLException {
            Integer count = Integer.valueOf("0");
            ProjectionList projectionList = Projections.projectionList();
            Criteria criteria = session.createCriteria(persistentClass);

            processJoinPath(criteria, joinPaths, projectionList);
            if (params != null) {
                for (Criterion criterion : params) {
                    criteria.add(criterion);
                }//from w w w. j av  a2s  . c o  m
            }
            //jika projectionList tidak kosong berarti ada GROUPING, lakukan konversi criteria
            if (projectionList.getLength() > 0) {
                criteria.setProjection(projectionList);
                String sql = criteriaToString(criteria);
                sql = "select count(*) from (" + sql + ") as countTemp";
                //native SQL dijalankan
                SQLQuery sqlQuery = session.createSQLQuery(sql);
                List result = sqlQuery.list();
                if (result != null && result.size() > 0) {
                    count = (Integer) result.get(0);
                }

            } else {
                criteria.setProjection(projectionList.add(Projections.rowCount()));
                List result = criteria.list();
                if (result != null && result.size() > 0) {
                    count = (Integer) result.get(0);
                }
            }
            return count;
        }
    });
}

From source file:org.codehaus.groovy.grails.orm.hibernate.query.AbstractHibernateQuery.java

License:Apache License

@Override
public List list() {
    if (criteria == null)
        throw new IllegalStateException("Cannot execute query using a detached criteria instance");

    int projectionLength = 0;
    if (hibernateProjectionList != null) {
        org.hibernate.criterion.ProjectionList projectionList = hibernateProjectionList
                .getHibernateProjectionList();
        projectionLength = projectionList.getLength();
        criteria.setProjection(projectionList);
    }// ww w . j  a  v  a 2  s .  c om

    if (projectionLength < 2) {
        criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
    }

    applyFetchStrategies();
    return criteria.list();
}

From source file:org.grails.orm.hibernate.query.AbstractHibernateQuery.java

License:Apache License

@Override
public List list() {
    if (criteria == null)
        throw new IllegalStateException("Cannot execute query using a detached criteria instance");

    int projectionLength = 0;
    if (hibernateProjectionList != null) {
        org.hibernate.criterion.ProjectionList projectionList = hibernateProjectionList
                .getHibernateProjectionList();
        projectionLength = projectionList.getLength();
        if (projectionLength > 0) {
            criteria.setProjection(projectionList);
        }/*from   www .  j av  a2 s. c o  m*/
    }

    if (projectionLength < 2) {
        criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
    }

    applyDefaultSortOrderAndCaching();
    applyFetchStrategies();

    return criteria.list();
}

From source file:org.LexGrid.LexBIG.caCore.dao.orm.translators.GridCQLToDetachedCriteria.java

License:Open Source License

private DetachedCriteria handleQueryOptions(DetachedCriteria criteria, QueryModifier modifiers) {
    if (modifiers == null) {
        return criteria;
    }/*from w  ww  .  j  a v  a2s .com*/

    ProjectionList projectionList = Projections.projectionList();

    String[] projectionProperties = modifiers.getAttributeNames();

    if (projectionProperties != null) {
        for (String prop : projectionProperties) {
            projectionList.add(Projections.property(prop));
        }
    }

    String distinctAttribute = modifiers.getDistinctAttribute();
    if (distinctAttribute != null) {
        projectionList.add(Projections.distinct(Projections.property(distinctAttribute)));
    }

    boolean isCount = modifiers.isCountOnly();
    if (isCount) {
        projectionList.add(Projections.rowCount());
    }

    //Only add the Projection List if it was populated with something.
    if (projectionList.getLength() > 0) {
        criteria.setProjection(projectionList);
    }
    return criteria;
}