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, String alias) 

Source Link

Document

Adds a projection to this list of projections after wrapping it with an alias

Usage

From source file:com.ar.dev.tierra.api.dao.impl.ProductoDAOImpl.java

@SuppressWarnings("unchecked")
@Override/*from   w ww. j  av a 2s.  c o m*/
public List<Producto> getAll() {
    Criteria criteria = getSession().createCriteria(Producto.class);
    ProjectionList projList = Projections.projectionList();
    projList.add(Projections.property("idProducto"), "idProducto");
    projList.add(Projections.property("marcas"), "marcas");
    projList.add(Projections.property("descripcion"), "descripcion");
    projList.add(Projections.property("colorProducto"), "colorProducto");
    projList.add(Projections.property("cantidadTotal"), "cantidadTotal");
    projList.add(Projections.property("talla"), "talla");
    criteria.add(Restrictions.eq("estadoProducto", true));
    criteria.addOrder(Order.desc("idProducto"));
    criteria.setProjection(Projections.distinct(projList));
    criteria.setResultTransformer(Transformers.aliasToBean(Producto.class));
    List<Producto> list = criteria.list();
    return list;
}

From source file:com.ar.dev.tierra.api.dao.impl.UsuariosDAOImpl.java

@SuppressWarnings("unchecked")
@Override/*from  w w  w  .  j a v a 2 s  . co  m*/
public List<Usuarios> allUsuarios() {
    Criteria criteria = getSession().createCriteria(Usuarios.class);
    criteria.addOrder(Order.asc("idUsuario"));
    ProjectionList projList = Projections.projectionList();
    projList.add(Projections.property("idUsuario"), "idUsuario");
    projList.add(Projections.property("roles"), "roles");
    projList.add(Projections.property("nombre"), "nombre");
    projList.add(Projections.property("apellido"), "apellido");
    projList.add(Projections.property("dni"), "dni");
    projList.add(Projections.property("telefono"), "telefono");
    projList.add(Projections.property("email"), "email");
    projList.add(Projections.property("fechaNacimiento"), "fechaNacimiento");
    projList.add(Projections.property("domicilio"), "domicilio");
    projList.add(Projections.property("estado"), "estado");
    projList.add(Projections.property("ultimaConexion"), "ultimaConexion");
    projList.add(Projections.property("usuarioSucursal"), "usuarioSucursal");
    criteria.setProjection(Projections.distinct(projList));
    criteria.setResultTransformer(Transformers.aliasToBean(Usuarios.class));
    List<Usuarios> us = criteria.list();
    return us;
}

From source file:com.denimgroup.threadfix.data.dao.hibernate.HibernateVulnerabilitySearchDao.java

License:Mozilla Public License

@Override
public List<Map> getScanComparison(VulnerabilitySearchParameters parameters, boolean isFalsePositive) {
    assert parameters != null;
    List<Integer> idList = getVulnIdList(parameters);

    if (idList.isEmpty())
        return list();

    Session session = sessionFactory.getCurrentSession();

    List<Map> fullList = list();

    // TODO refactor this to reduce duplication or remove the need for it
    int current = 0;
    while (current < idList.size()) {

        int start = current, end = current + 500;
        if (end > idList.size()) {
            end = idList.size();//  ww  w.  j  av a2s.  com
        }

        List<Integer> thisPage = idList.subList(start, end);

        Criteria criteria = session.createCriteria(Vulnerability.class);

        criteria.createAlias("findings", "finding");
        criteria.createAlias("finding.scan", "scan");
        criteria.createAlias("scan.applicationChannel", "applicationChannel");
        criteria.createAlias("applicationChannel.channelType", "channelType");
        criteria.add(Restrictions.in("id", thisPage));

        ProjectionList projectionList = Projections.projectionList()
                .add(Projections.groupProperty("channelType.name"), "channelName")
                .add(Projections.alias(Projections.countDistinct("id"), "foundCount"));

        if (!isFalsePositive) {
            projectionList.add(Projections.groupProperty("foundHAMEndpoint"), "foundHAMEndpoint");
        }
        criteria.setProjection(projectionList);
        criteria.addOrder(Order.desc("foundCount"));
        criteria.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);

        List results = (List<Map>) criteria.list();
        fullList.addAll(results);

        current += 500;
    }

    return fullList;

}

From source file:com.heliosapm.aa4h.parser.XMLQueryParser.java

License:Apache License

/**
 * Adds a new projection to the current projectionList stack.
 * Mandatory Attributes:<ul>/*from   ww  w.  j a  v a  2  s . c  o  m*/
 * <li><b>name</b>: The field to create a projection on.</li>
 * <li><b>type</b>: The projection type.</li>
 * <li><b>alias</b>: The name applied to the projection returned field.</li>
 * </ul>
 * @param attrs The attributes of the processed node.
 * @throws SAXException
 * TODO: Implement checks for mandatory attributes.
 */
public void addProjection(Attributes attrs) throws SAXException {
    ProjectionList projectionList = projectionStack.peek();
    if (projectionList == null) {
        throw new SAXException(
                "Attempted to add Projection and no ProjectionList Exists On The Stack. (Projection must be embedded inside a Projections)");
    }
    String type = attrs.getValue("type");
    String name = attrs.getValue("name");
    String alias = attrs.getValue("alias");
    if ("avg".equalsIgnoreCase(type)) {
        projectionList.add(Projections.avg(name), alias);
    } else if ("count".equalsIgnoreCase(type)) {
        projectionList.add(Projections.count(name), alias);
    } else if ("countDistinct".equalsIgnoreCase(type)) {
        projectionList.add(Projections.countDistinct(name), alias);
    } else if ("groupProperty".equalsIgnoreCase(type)) {
        projectionList.add(Projections.groupProperty(name), alias);
    } else if ("max".equalsIgnoreCase(type)) {
        projectionList.add(Projections.max(name), alias);
    } else if ("min".equalsIgnoreCase(type)) {
        projectionList.add(Projections.min(name), alias);
    } else if ("sum".equalsIgnoreCase(type)) {
        projectionList.add(Projections.sum(name), alias);
    } else if ("rowCount".equalsIgnoreCase(type)) {
        projectionList.add(Projections.rowCount(), alias);
    }

}

From source file:com.maydesk.base.table.PDPageableFactory.java

License:Mozilla Public License

public Projection getProjectionList() {
    ProjectionList projectionList = Projections.projectionList();
    projectionList.add(Projections.id(), "id");
    projectionList.add(Projections.property("cachedTitle"), "title"); // Model must be of type MBaseWithTitle!
    projectionList.add(Projections.property("cachedDescription"), "description"); // Model must be of type MBaseWithTitle!
    return Projections.distinct(projectionList);
}

From source file:com.romeikat.datamessie.core.base.dao.impl.AbstractEntityWithIdAndVersionDao.java

License:Open Source License

@Override
public TreeMap<Long, Long> getIdsWithVersion(final SharedSessionContract ssc, final Long firstId,
        final Integer maxResults) {
    // Query/* w  w  w. j a v a  2  s  . c o m*/
    final EntityWithIdQuery<E> query = new EntityWithIdQuery<>(getEntityClass());
    if (firstId != null) {
        query.addRestriction(Restrictions.ge("id", firstId));
    }
    query.setMaxResults(maxResults);
    query.addOrder(Order.asc("id"));
    query.setResultTransformer(new AliasToBeanResultTransformer(IdAndVersion.class));

    // Done
    final ProjectionList projectionList = Projections.projectionList();
    projectionList.add(Projections.property("id"), "id");
    projectionList.add(Projections.property("version"), "version");
    @SuppressWarnings("unchecked")
    final List<IdAndVersion> idsAndVersions = (List<IdAndVersion>) query.listForProjection(ssc, projectionList);

    // Transform into map
    final TreeMap<Long, Long> result = transformIntoMap(idsAndVersions);
    return result;
}

From source file:com.romeikat.datamessie.core.base.dao.impl.AbstractEntityWithIdAndVersionDao.java

License:Open Source License

@Override
public TreeMap<Long, Long> getIdsWithVersion(final SharedSessionContract ssc, final Collection<Long> ids) {
    if (ids.isEmpty()) {
        return new TreeMap<Long, Long>();
    }/*from   ww w . j  a v a 2s  .c  o m*/

    // Query
    final EntityWithIdQuery<E> query = new EntityWithIdQuery<>(getEntityClass());
    query.addRestriction(Restrictions.in("id", ids));
    query.setResultTransformer(new AliasToBeanResultTransformer(IdAndVersion.class));

    // Done
    final ProjectionList projectionList = Projections.projectionList();
    projectionList.add(Projections.property("id"), "id");
    projectionList.add(Projections.property("version"), "version");
    @SuppressWarnings("unchecked")
    final List<IdAndVersion> idsAndVersions = (List<IdAndVersion>) query.listForProjection(ssc, projectionList);

    // Transform into map
    final TreeMap<Long, Long> result = transformIntoMap(idsAndVersions);
    return result;
}

From source file:com.romeikat.datamessie.core.base.dao.impl.CrawlingDao.java

License:Open Source License

public List<CrawlingDto> getAsDtos(final SharedSessionContract ssc, final Long projectId) {
    // Query: Crawling
    final EntityWithIdQuery<Crawling> crawlingQuery = new EntityWithIdQuery<>(Crawling.class);
    crawlingQuery.addRestriction(Restrictions.eqOrIsNull("projectId", projectId));
    crawlingQuery.addOrder(Order.desc("started"));
    crawlingQuery.setResultTransformer(new AliasToBeanResultTransformer(CrawlingDto.class));

    // Done/*w  w  w.jav  a2 s. com*/
    final ProjectionList projectionList = Projections.projectionList();
    projectionList.add(Projections.groupProperty("id"), "id");
    projectionList.add(Projections.property("started"), "started");
    projectionList.add(Projections.property("completed"), "completed");
    @SuppressWarnings("unchecked")
    final List<CrawlingDto> dtos = (List<CrawlingDto>) crawlingQuery.listForProjection(ssc, projectionList);

    // Set duration
    setDurationForDtos(dtos);

    return dtos;
}

From source file:com.romeikat.datamessie.core.base.dao.impl.CrawlingDao.java

License:Open Source License

public List<CrawlingOverviewDto> getAsOverviewDtos(final SharedSessionContract ssc, final Long projectId,
        final Long first, final Long count) {
    // Query: Crawling
    final EntityWithIdQuery<Crawling> crawlingQuery = new EntityWithIdQuery<>(Crawling.class);
    crawlingQuery.addRestriction(Restrictions.eqOrIsNull("projectId", projectId));
    crawlingQuery.setFirstResult(first == null ? null : first.intValue());
    crawlingQuery.setMaxResults(count == null ? null : count.intValue());
    crawlingQuery.addOrder(Order.desc("started"));
    crawlingQuery.setResultTransformer(new AliasToBeanResultTransformer(CrawlingOverviewDto.class));

    // Done//from  w w  w .  java 2 s .  c  om
    final ProjectionList projectionList = Projections.projectionList();
    projectionList.add(Projections.groupProperty("id"), "id");
    projectionList.add(Projections.property("started"), "started");
    projectionList.add(Projections.property("completed"), "completed");
    @SuppressWarnings("unchecked")
    final List<CrawlingOverviewDto> dtos = (List<CrawlingOverviewDto>) crawlingQuery.listForProjection(ssc,
            projectionList);

    // Set duration
    setDurationForOverviewDtos(dtos);

    return dtos;
}

From source file:com.romeikat.datamessie.core.base.dao.impl.ProjectDao.java

License:Open Source License

public List<ProjectDto> getAllAsDtos(final SharedSessionContract ssc, final Long userId) {
    // Restrict to user
    final Collection<Long> projectIdsForUser = getIdsForUser(ssc, userId);
    if (projectIdsForUser.isEmpty()) {
        return Collections.emptyList();
    }//from w  w w .  java  2  s.c o  m

    // Query: Project
    final EntityWithIdQuery<Project> projectQuery = new EntityWithIdQuery<>(Project.class);
    projectQuery.addIdRestriction(projectIdsForUser);
    projectQuery.addOrder(Order.asc("name"));
    projectQuery.setResultTransformer(new AliasToBeanResultTransformer(ProjectDto.class));

    // Done
    final ProjectionList projectionList = Projections.projectionList();
    projectionList.add(Projections.property("id"), "id");
    projectionList.add(Projections.property("name"), "name");
    projectionList.add(Projections.property("crawlingEnabled"), "crawlingEnabled");
    projectionList.add(Projections.property("crawlingInterval"), "crawlingInterval");
    projectionList.add(Projections.property("preprocessingEnabled"), "preprocessingEnabled");
    @SuppressWarnings("unchecked")
    final List<ProjectDto> dtos = (List<ProjectDto>) projectQuery.listForProjection(ssc, projectionList);
    return dtos;
}