Example usage for org.hibernate Criteria setResultTransformer

List of usage examples for org.hibernate Criteria setResultTransformer

Introduction

In this page you can find the example usage for org.hibernate Criteria setResultTransformer.

Prototype

public Criteria setResultTransformer(ResultTransformer resultTransformer);

Source Link

Document

Set a strategy for handling the query results.

Usage

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

License:Mozilla Public License

@Override
public List<VulnerabilityTreeElement> getTree(VulnerabilitySearchParameters parameters) {
    assert parameters != null;

    Criteria criteria = VulnerabilitySearchCriteriaConstructor
            .getCriteriaWithRestrictions(sessionFactory.getCurrentSession(), parameters);

    criteria.setProjection(Projections.projectionList().add(Projections.countDistinct("id"), "numResults")
            .add(Projections.groupProperty("severity.intValue"))
            .add(Projections.groupProperty("genericVulnAlias.id"), "genericVulnerabilityId")
            .add(Projections.groupProperty("genericVulnAlias.name"), "genericVulnerabilityName")
            .add(Projections.groupProperty("genericVulnAlias.cweId"), "genericVulnerabilityDisplayId")
            .add(Projections.groupProperty("severity.intValue"), "severityIntValue"))
            .addOrder(Order.desc("severityIntValue")).addOrder(Order.desc("numResults"));

    criteria.setResultTransformer(Transformers.aliasToBean(VulnerabilityTreeElement.class));

    return (List<VulnerabilityTreeElement>) criteria.list();
}

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();//from w  w  w  . j  a  va 2 s .  c  om
        }

        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.dosport.hibernate.utils.HibernateDao.java

License:Apache License

/**
 * countCriteria.//from w ww . j  a va2s.  c  o m
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
protected long countCriteriaResult(final Criteria c) {
    CriteriaImpl impl = (CriteriaImpl) c;

    // Projection?ResultTransformer?OrderBy??,??Count?
    Projection projection = impl.getProjection();
    ResultTransformer transformer = impl.getResultTransformer();

    List<CriteriaImpl.OrderEntry> orderEntries = null;
    try {
        orderEntries = (List) ReflectionUtils.getFieldValue(impl, "orderEntries");
        ReflectionUtils.setFieldValue(impl, "orderEntries", new ArrayList());
    } catch (Exception e) {
        logger.error("??:{}", e.getMessage());
    }

    // Count
    Long totalCountObject = (Long) c.setProjection(Projections.rowCount()).uniqueResult();
    long totalCount = (totalCountObject != null) ? totalCountObject : 0;

    // ?Projection,ResultTransformerOrderBy??
    c.setProjection(projection);

    if (projection == null) {
        c.setResultTransformer(CriteriaSpecification.ROOT_ENTITY);
    }
    if (transformer != null) {
        c.setResultTransformer(transformer);
    }
    try {
        ReflectionUtils.setFieldValue(impl, "orderEntries", orderEntries);
    } catch (Exception e) {
        logger.error("??:{}", e.getMessage());
    }

    return totalCount;
}

From source file:com.edgenius.core.dao.hibernate.BaseDAOHibernate.java

License:Open Source License

public List<T> getObjects() {

    Criteria criteria = getCurrentSesssion().createCriteria(entityClass);
    criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
    return criteria.list();

}

From source file:com.eryansky.common.orm.core.hibernate.support.HibernateSupportDao.java

License:Apache License

/**
 * countCriteria.//from  w w w  .  j av a 2s . c  o m
 * 
 * @param c Criteria
 * 
 * @return long
 */
protected long countCriteriaResult(Criteria c) {
    CriteriaImpl impl = (CriteriaImpl) c;

    // Projection?ResultTransformer?OrderBy??,??Count?
    Projection projection = impl.getProjection();
    ResultTransformer transformer = impl.getResultTransformer();

    List<CriteriaImpl.OrderEntry> orderEntries = null;
    try {
        orderEntries = (List) ReflectionUtils.getFieldValue(impl, "orderEntries");
        ReflectionUtils.setFieldValue(impl, "orderEntries", new ArrayList());
    } catch (Exception e) {
        e.printStackTrace();
    }

    // Count
    Long totalCountObject = (Long) c.setProjection(Projections.rowCount()).uniqueResult();
    long totalCount = (totalCountObject != null) ? totalCountObject : 0;

    // ?Projection,ResultTransformerOrderBy??
    c.setProjection(projection);

    if (projection == null) {
        c.setResultTransformer(CriteriaSpecification.ROOT_ENTITY);
    }
    if (transformer != null) {
        c.setResultTransformer(transformer);
    }

    try {
        ReflectionUtils.setFieldValue(impl, "orderEntries", orderEntries);
    } catch (Exception e) {
        e.printStackTrace();
    }

    return totalCount;
}

From source file:com.evolveum.midpoint.repo.sql.query.custom.ShadowQueryWithDisjunction.java

License:Apache License

@Override
public RQuery createQuery(ObjectQuery objectQuery, Class<? extends ObjectType> type,
        Collection<SelectorOptions<GetOperationOptions>> options, boolean countingObjects, Session session) {

    DetachedCriteria c1 = DetachedCriteria.forClass(ClassMapper.getHQLTypeClass(ShadowType.class), "s");
    c1.createCriteria("strings", "s1", JoinType.LEFT_OUTER_JOIN);

    ParsedQuery parsedQuery = parse(objectQuery);
    Conjunction conjunction = Restrictions.conjunction();
    conjunction//from  w w w . j  a  v a2  s.co  m
            .add(Restrictions.eq("resourceRef.targetOid", parsedQuery.refFilter.getValues().get(0).getOid()));
    Disjunction disjunction = Restrictions.disjunction();
    disjunction.add(createAttributeEq(parsedQuery.eqUidFilter,
            parsedQuery.eqUidFilter.getPath().lastNamed().getName()));
    disjunction.add(createAttributeEq(parsedQuery.eqNameFilter, SchemaConstantsGenerated.ICF_S_NAME));
    conjunction.add(disjunction);
    c1.add(conjunction);

    if (countingObjects) {
        c1.setProjection(Projections.countDistinct("s.oid"));
        return new RQueryCriteriaImpl(c1.getExecutableCriteria(session));
    }

    c1.setProjection(Projections.distinct(Projections.property("s.oid")));

    Criteria cMain = session.createCriteria(ClassMapper.getHQLTypeClass(ShadowType.class), "o");
    cMain.add(Subqueries.propertyIn("oid", c1));

    if (objectQuery != null && objectQuery.getPaging() != null) {
        cMain = updatePagingAndSorting(cMain, type, objectQuery.getPaging());
    }

    ProjectionList projections = Projections.projectionList();
    projections.add(Projections.property("fullObject"));
    projections.add(Projections.property("stringsCount"));
    projections.add(Projections.property("longsCount"));
    projections.add(Projections.property("datesCount"));
    projections.add(Projections.property("referencesCount"));
    projections.add(Projections.property("polysCount"));
    projections.add(Projections.property("booleansCount"));

    cMain.setProjection(projections);

    cMain.setResultTransformer(GetObjectResult.RESULT_TRANSFORMER);
    return new RQueryCriteriaImpl(cMain);
}

From source file:com.ewabackend.dao.GroupDAOImplementation.java

@Override
public Group findByName(String name) {
    Criteria criteria = createEntityCriteria();
    criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
    criteria.add(Restrictions.eq("name", "ID2S01"));
    return (Group) criteria.uniqueResult();
}

From source file:com.ewabackend.dao.GroupDAOImplementation.java

@Override
public List<Group> findByYear(Integer year) {
    Criteria criteria = createEntityCriteria();
    criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
    criteria.add(Restrictions.eq("year", year));
    return (List<Group>) (Group) criteria.list();
}

From source file:com.ewabackend.dao.GroupDAOImplementation.java

@Override
public List<Group> findAllGroups() {
    Criteria criteria = createEntityCriteria().addOrder(Order.asc("year"));
    criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
    List<Group> groups = (List<Group>) criteria.list();
    return groups;
}

From source file:com.ewabackend.dao.GroupHasSubjectDAOImplementation.java

@Override
public List<GroupHasSubject> findAllGroupHasSubject() {
    Criteria criteria = createEntityCriteria().addOrder(Order.asc("id"));
    criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
    List<GroupHasSubject> results = (List<GroupHasSubject>) criteria.list();
    return results;
}