Example usage for org.hibernate.criterion ProjectionList add

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

Introduction

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

Prototype

public ProjectionList add(Projection projection) 

Source Link

Document

Add a projection to this list of projections

Usage

From source file:br.com.prototipo.dao.ProfessorDao.java

public List<String> getListProfessoresString() {
    if (sessao.isConnected()) {
        sessao.close();/*from   ww  w .  j a  v  a  2s .c  o m*/
    }
    sessao = HibernateUtil.getSessionFactory().openSession();

    Criteria cri = sessao.createCriteria(Professor.class);
    Criteria cri2 = sessao.createCriteria(Professor.class);
    cri.addOrder(Order.asc("registro"));
    cri2.addOrder(Order.asc("registro"));

    ProjectionList projection = Projections.projectionList();
    projection.add(Projections.property("registro"));

    ProjectionList projection2 = Projections.projectionList();
    projection2.add(Projections.property("nome"));

    cri.setProjection(projection);
    cri2.setProjection(projection2);

    List<String> l1 = cri.list();
    List<String> l2 = cri2.list();
    List<String> l3 = new ArrayList<>();
    Iterator<String> it1 = l1.iterator();
    Iterator<String> it2 = l2.iterator();

    while (it1.hasNext()) {
        String aux1 = it1.next();
        String aux2 = it2.next();
        String aux3 = aux1 + " - " + aux2;
        l3.add(aux3);
    }

    return l3;
}

From source file:br.com.prototipo.dao.TurmaDao.java

public List<String> getListTurmasString() {
    if (sessao.isConnected()) {
        sessao.close();/*from   ww w. ja v  a2 s .com*/
    }
    sessao = HibernateUtil.getSessionFactory().openSession();

    Criteria cri = sessao.createCriteria(Turma.class);
    cri.addOrder(Order.asc("codigo"));

    Criteria cri2 = sessao.createCriteria(Turma.class);
    cri2.addOrder(Order.asc("codigo"));

    ProjectionList projection = Projections.projectionList();
    projection.add(Projections.property("codigo"));
    cri.setProjection(projection);

    ProjectionList projection2 = Projections.projectionList();
    projection2.add(Projections.property("nomeDisciplina"));
    cri2.setProjection(projection2);

    List<String> l1 = cri.list();
    List<String> l2 = cri2.list();
    List<String> l3 = new ArrayList<>();
    Iterator<String> it1 = l1.iterator();
    Iterator<String> it2 = l2.iterator();
    while (it1.hasNext()) {
        String aux1 = it1.next();
        String aux2 = it2.next();
        String aux3 = aux1 + " - " + aux2;
        l3.add(aux3);
    }

    return l3;
}

From source file:br.gov.mdarte.controleacesso.cd.PerfilDAOImpl.java

@Override
protected Object handleRecuperarSuperPerfil(Session session, String sistema, Integer paginacao, Integer linhas,
        Integer paginas) throws DAOException {
    ProjectionList projectionList = Projections.projectionList();

    projectionList.add(Projections.property("id"));

    Criteria criterios = session.createCriteria(PerfilImpl.class);
    criterios.createAlias("sistema", "sistema");

    criterios.add(Restrictions.eq("superUsuario", Boolean.TRUE));

    criterios.setProjection(projectionList);

    if (sistema != null)
        criterios.add(Restrictions.eq("sistema.nome", sistema));

    ResultTransformer resultTransformer = new ResultTransformer() {

        public Object transformTuple(Object[] result, String[] arg1) {
            return result;
        }//from   w  w w. j a v a  2  s. c om

        public List transformList(List resultado) {
            List idPerfis = new ArrayList<Long>();

            for (Object[] result : (List<Object[]>) resultado) {
                Collection rows = Arrays.asList(result);
                Iterator iteratorRow = rows.iterator();

                idPerfis.add((Long) iteratorRow.next());
            }

            return idPerfis;
        }
    };

    criterios.setResultTransformer(resultTransformer);

    return criterios;
}

From source file:ch.systemsx.cisd.openbis.generic.server.dataaccess.db.EntityPropertyTypeDAO.java

License:Apache License

public void fillTermUsageStatistics(List<VocabularyTermWithStats> termsWithStats, VocabularyPE vocabulary) {
    assert termsWithStats != null : "Unspecified terms.";
    assert vocabulary != null : "Unspecified vocabulary.";
    assert termsWithStats.size() == vocabulary.getTerms()
            .size() : "Sizes of terms to be filled and vocabulary terms don't match.";

    Map<Long, VocabularyTermWithStats> termsById = new HashMap<Long, VocabularyTermWithStats>(
            termsWithStats.size());/*from ww  w  .  j  a va2s. c o  m*/
    for (VocabularyTermWithStats termWithStats : termsWithStats) {
        Long id = termWithStats.getTerm().getId();
        termsById.put(id, termWithStats);
    }

    final DetachedCriteria criteria = DetachedCriteria.forClass(entityKind.getEntityPropertyClass());
    // alias is the easiest way to restrict on association using criteria
    criteria.createAlias("vocabularyTerm", "term");
    criteria.add(Restrictions.eq("term.vocabularyInternal", vocabulary));
    ProjectionList projectionList = Projections.projectionList();
    projectionList.add(Projections.rowCount());
    projectionList.add(Projections.groupProperty("term.id"));
    criteria.setProjection(projectionList);

    final List<Object[]> results = cast(getHibernateTemplate().findByCriteria(criteria));

    for (Object[] result : results) {
        Integer numberOfUsages = (Integer) result[0];
        Long termId = (Long) result[1];
        termsById.get(termId).registerUsage(entityKind, numberOfUsages);
    }
}

From source file:com.abiquo.server.core.infrastructure.network.IpPoolManagementDAO.java

License:Open Source License

public Collection<String> getAllMacs() {
    Criteria criteria = getSession().createCriteria(IpPoolManagement.class);
    ProjectionList projList = Projections.projectionList();
    projList.add(Projections.property(IpPoolManagement.MAC_PROPERTY));

    criteria.setProjection(projList);/*from   www .  j  av a  2 s.c o m*/
    return criteria.list();
}

From source file:com.abiquo.server.core.statistics.CloudUsageDAO.java

License:Open Source License

public CloudUsage sumTotalCloudUsage() {
    // TODO: Include aggregators functionality in bzengine?
    Session ses = HibernateEntityManagerHelper.getSession(getEntityManager());

    Criteria crit = ses.createCriteria(CloudUsage.class);
    ProjectionList proList = Projections.projectionList();
    proList.add(Projections.sum(CloudUsage.SERVERS_TOTAL_PROPERTY));
    proList.add(Projections.sum(CloudUsage.SERVERS_RUNNING_PROPERTY));
    proList.add(Projections.sum(CloudUsage.STORAGE_TOTAL_PROPERTY));
    // proList.add(Projections.sum(CloudUsage.STORAGE_RESERVED_PROPERTY));
    proList.add(Projections.sum(CloudUsage.STORAGE_USED_PROPERTY));
    proList.add(Projections.sum(CloudUsage.PUBLIC_I_PS_TOTAL_PROPERTY));
    // proList.add(Projections.sum(CloudUsage.PUBLIC_I_PS_RESERVED_PROPERTY));
    proList.add(Projections.sum(CloudUsage.PUBLIC_I_PS_USED_PROPERTY));
    proList.add(Projections.sum(CloudUsage.V_MACHINES_TOTAL_PROPERTY));
    proList.add(Projections.sum(CloudUsage.V_MACHINES_RUNNING_PROPERTY));
    proList.add(Projections.sum(CloudUsage.V_CPU_TOTAL_PROPERTY));
    // proList.add(Projections.sum(CloudUsage.V_CPU_RESERVED_PROPERTY));
    proList.add(Projections.sum(CloudUsage.V_CPU_USED_PROPERTY));
    proList.add(Projections.sum(CloudUsage.V_MEMORY_TOTAL_PROPERTY));
    // proList.add(Projections.sum(CloudUsage.V_MEMORY_RESERVED_PROPERTY));
    proList.add(Projections.sum(CloudUsage.V_MEMORY_USED_PROPERTY));
    proList.add(Projections.sum(CloudUsage.V_STORAGE_TOTAL_PROPERTY));
    // proList.add(Projections.sum(CloudUsage.V_STORAGE_RESERVED_PROPERTY));
    proList.add(Projections.sum(CloudUsage.V_STORAGE_USED_PROPERTY));
    proList.add(Projections.sum(CloudUsage.NUM_USERS_CREATED_PROPERTY));
    proList.add(Projections.sum(CloudUsage.NUM_VDC_CREATED_PROPERTY));
    proList.add(Projections.sum(CloudUsage.NUM_ENTERPRISES_CREATED_PROPERTY));

    crit.setProjection(proList);/* www .  ja  v a2s .  c o  m*/
    Object[] obj = (Object[]) crit.uniqueResult(); // Returns Object[] ->
    CloudUsage result = new CloudUsage();

    int cont = 0;
    result.setServersTotal((Long) obj[cont++]);
    result.setServersRunning((Long) obj[cont++]);
    result.setStorageTotal((Long) obj[cont++]);
    // result.setStorageReserved((Long) obj[cont++]);
    result.setStorageUsed((Long) obj[cont++]);
    result.setPublicIPsTotal((Long) obj[cont++]);
    // result.setPublicIPsReserved((Long) obj[cont++]);
    result.setPublicIPsUsed((Long) obj[cont++]);
    result.setVirtualMachinesTotal((Long) obj[cont++]);
    result.setVirtualMachinesRunning((Long) obj[cont++]);
    result.setVirtualCpuTotal((Long) obj[cont++]);
    // result.setVirtualCpuReserved((Long) obj[cont++]);
    result.setVirtualCpuUsed((Long) obj[cont++]);
    result.setVirtualMemoryTotal((Long) obj[cont++]);
    // result.setVirtualMemoryReserved((Long) obj[cont++]);
    result.setVirtualMemoryUsed((Long) obj[cont++]);
    result.setVirtualStorageTotal((Long) obj[cont++]);
    // result.setVirtualStorageReserved((Long) obj[cont++]);
    result.setVirtualStorageUsed((Long) obj[cont++]);
    result.setNumUsersCreated((Long) obj[cont++]);
    result.setNumVdcCreated((Long) obj[cont++]);
    result.setNumEnterprisesCreated((Long) obj[cont++]);

    return result;
}

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

License:Open Source License

@Override
public StorageResults visit(Distinct distinct) {
    // Standard visit for the expression where distinct should be added
    distinct.getExpression().accept(this);
    // Wraps the last projection into a 'distinct' statement
    // Note: Hibernate does not provide projection editing functions... have to work around that with a new
    // projection list.
    ProjectionList newProjectionList = Projections.projectionList();
    int i = 0;// w  ww  .  j a  va 2  s.  com
    for (; i < projectionList.getLength() - 1; i++) {
        newProjectionList.add(projectionList.getProjection(i));
    }
    newProjectionList.add(Projections.distinct(projectionList.getProjection(i)));
    projectionList = newProjectionList;
    return null;
}

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   www .j  a  v  a 2  s  . c  o m
    } 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.amalto.core.storage.hibernate.StandardQueryHandler.java

License:Open Source License

@Override
public StorageResults visit(OrderBy orderBy) {
    TypedExpression orderByExpression = orderBy.getExpression();
    CriterionFieldCondition fieldCondition = new CriterionFieldCondition();
    FieldCondition condition = orderByExpression.accept(fieldCondition);
    if (orderByExpression instanceof Field) {
        Field field = (Field) orderByExpression;
        FieldMetadata userFieldMetadata = field.getFieldMetadata();
        ComplexTypeMetadata containingType = getContainingType(userFieldMetadata);
        Set<String> aliases = getAliases(containingType, field);
        condition.criterionFieldNames = new ArrayList<>(aliases.size());
        for (String alias : aliases) {
            addCondition(condition, alias, userFieldMetadata);
        }/* w w  w. ja  v a 2 s.  c o  m*/
    }
    if (orderByExpression instanceof Count) {
        Count count = (Count) orderByExpression;
        String propertyName = count.getExpression().accept(fieldCondition).criterionFieldNames.get(0);
        ProjectionList list = projectionList;
        if (projectionList instanceof ReadOnlyProjectionList) {
            list = ((ReadOnlyProjectionList) projectionList).inner();
        }
        list.add(Projections.groupProperty(propertyName));
        String alias = "x_talend_countField" + countAggregateIndex++; //$NON-NLS-1$
        list.add(Projections.count(propertyName).as(alias));
        switch (orderBy.getDirection()) {
        case ASC:
            criteria.addOrder(Order.asc(alias));
            break;
        case DESC:
            criteria.addOrder(Order.desc(alias));
            break;
        }
    }
    if (condition != null) {
        for (String fieldName : condition.criterionFieldNames) {
            OrderBy.Direction direction = orderBy.getDirection();
            switch (direction) {
            case ASC:
                criteria.addOrder(Order.asc(fieldName));
                break;
            case DESC:
                criteria.addOrder(Order.desc(fieldName));
                break;
            }
        }
    }
    return null;
}

From source file:com.ateam.hibernate.HibernateDAOImpl.java

public String checkRole(String strUserName, String password) throws DataAccessException, java.sql.SQLException {
    String obj = null;//from w  w  w.  jav  a 2 s  .c om
    DetachedCriteria critthree = DetachedCriteria.forClass(UserAttr.class);
    ProjectionList pl = Projections.projectionList();
    pl.add(Projections.groupProperty("userRole"));
    critthree.add(Restrictions.eq("userName", strUserName));
    critthree.add(Restrictions.eq("userPassword", password));
    critthree.setProjection(pl);
    List objs = getHibernateTemplate().findByCriteria(critthree);
    if ((objs != null) && (objs.size() > 0)) {
        obj = (String) objs.get(0);
    }
    return obj;
}