Example usage for org.hibernate.criterion CriteriaSpecification ROOT_ENTITY

List of usage examples for org.hibernate.criterion CriteriaSpecification ROOT_ENTITY

Introduction

In this page you can find the example usage for org.hibernate.criterion CriteriaSpecification ROOT_ENTITY.

Prototype

ResultTransformer ROOT_ENTITY

To view the source code for org.hibernate.criterion CriteriaSpecification ROOT_ENTITY.

Click Source Link

Document

Each row of results is an instance of the root entity

Usage

From source file:jp.ac.tokushima_u.is.ll.common.orm.hibernate.HibernateDao.java

License:Apache License

/**
 * count???Criteria???????/*from w w  w . j av a 2  s  .  c o m*/
 */
@SuppressWarnings("unchecked")
protected long countCriteriaResult(final Criteria c) {
    CriteriaImpl impl = (CriteriaImpl) c;

    // ??Projection?ResultTransformer?OrderBy????NULL???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("Exception:{}", e.getMessage());
    }

    // Count?
    long totalCount = (Long) c.setProjection(Projections.rowCount()).uniqueResult();

    // ?????Projection?ResultTransformer?OrderBy????
    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("Exception:{}", e.getMessage());
    }

    return totalCount;
}

From source file:net.umpay.mailbill.hql.orm.hibernate.HibernateDao.java

License:Apache License

/**
 * countCriteria.// www .ja  v a2  s  .  com
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
protected int 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);
    }

    // Count
    int totalCount = (Integer) c.setProjection(Projections.rowCount()).uniqueResult();

    // ?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);
    }

    return totalCount;
}

From source file:net.umpay.mailbill.hql.orm.hibernate.HibernateDao.java

License:Apache License

/**
 * countCriteria.??//from  www . j  a v  a2 s .  c om
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
protected int countCriteriaResultByCache(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);
    }

    // Count
    int totalCount = (Integer) c.setProjection(Projections.rowCount()).setCacheable(true).uniqueResult();

    // ?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);
    }

    return totalCount;
}

From source file:org.jspresso.framework.model.persistence.hibernate.criterion.DefaultCriteriaFactory.java

License:Open Source License

/**
 * {@inheritDoc}//from w  w  w .j a  va2s .  c o  m
 */
@SuppressWarnings("ConstantConditions")
@Override
public void completeCriteriaWithOrdering(EnhancedDetachedCriteria criteria, IQueryComponent queryComponent,
        Map<String, Object> context) {
    criteria.setProjection(null);
    criteria.setResultTransformer(CriteriaSpecification.ROOT_ENTITY);
    // complete sorting properties
    if (queryComponent.getOrderingProperties() != null) {
        for (Map.Entry<String, ESort> orderingProperty : queryComponent.getOrderingProperties().entrySet()) {
            String propertyName = orderingProperty.getKey();
            String[] propElts = propertyName.split("\\.");
            DetachedCriteria orderingCriteria = criteria;
            boolean sortable = true;
            if (propElts.length > 1) {
                IComponentDescriptor<?> currentCompDesc = queryComponent.getQueryDescriptor();
                int i = 0;
                List<String> path = new ArrayList<>();
                for (; sortable && i < propElts.length - 1; i++) {
                    IReferencePropertyDescriptor<?> refPropDescriptor = ((IReferencePropertyDescriptor<?>) currentCompDesc
                            .getPropertyDescriptor(propElts[i]));
                    if (refPropDescriptor != null) {
                        sortable = sortable && isSortable(refPropDescriptor);
                        if (EntityHelper.isInlineComponentReference(refPropDescriptor)) {
                            break;
                        }
                        currentCompDesc = refPropDescriptor.getReferencedDescriptor();
                        path.add(propElts[i]);
                    } else {
                        LOG.error("Ordering property {} not found on {}", propElts[i],
                                currentCompDesc.getComponentContract().getName());
                        sortable = false;
                    }
                }
                if (sortable) {
                    StringBuilder name = new StringBuilder();
                    for (int j = i; sortable && j < propElts.length; j++) {
                        IPropertyDescriptor propDescriptor = currentCompDesc.getPropertyDescriptor(propElts[j]);
                        sortable = sortable && isSortable(propDescriptor);
                        if (j < propElts.length - 1) {
                            currentCompDesc = ((IReferencePropertyDescriptor<?>) propDescriptor)
                                    .getReferencedDescriptor();
                        }
                        if (j > i) {
                            name.append(".");
                        }
                        name.append(propElts[j]);
                    }
                    if (sortable) {
                        for (String pathElt : path) {
                            if (isUseAliasesForJoins()) {
                                orderingCriteria = criteria.getSubCriteriaFor(orderingCriteria, pathElt,
                                        pathElt, JoinType.LEFT_OUTER_JOIN);
                            } else {
                                orderingCriteria = criteria.getSubCriteriaFor(orderingCriteria, pathElt,
                                        JoinType.LEFT_OUTER_JOIN);
                            }
                        }
                        propertyName = name.toString();
                    }
                }
            } else {
                IPropertyDescriptor propertyDescriptor = queryComponent.getQueryDescriptor()
                        .getPropertyDescriptor(propertyName);
                if (propertyDescriptor != null) {
                    sortable = isSortable(propertyDescriptor);
                } else {
                    LOG.error("Ordering property {} not found on {}", propertyName,
                            queryComponent.getQueryDescriptor().getComponentContract().getName());
                    sortable = false;
                }
            }
            if (sortable) {
                Order order;
                switch (orderingProperty.getValue()) {
                case DESCENDING:
                    order = Order.desc(PropertyHelper.toJavaBeanPropertyName(propertyName));
                    break;
                case ASCENDING:
                default:
                    order = Order.asc(PropertyHelper.toJavaBeanPropertyName(propertyName));
                }
                orderingCriteria.addOrder(order);
            }
        }
    }
    // Query should always be ordered to preserve pagination.
    criteria.addOrder(Order.desc(IEntity.ID));
}

From source file:org.sakaiproject.qna.dao.impl.QnaDaoImpl.java

License:Educational Community License

/**
 * Search answers// w w  w  .  j  av  a  2  s  . c  o  m
 * 
 * @param search search query
 * @param location unique id of location
 * @return {@link List} of {@link QnaAnswer}
 */
@SuppressWarnings("unchecked")
public List<QnaAnswer> getSearchAnswers(String search, String location) {

    Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();
    Criteria criteria = session.createCriteria(QnaAnswer.class);
    criteria.add(Restrictions.ilike("answerText", search));
    criteria.createAlias("question", "question", Criteria.LEFT_JOIN);
    criteria.add(Restrictions.eq("question.location", location));
    criteria.setResultTransformer(CriteriaSpecification.ROOT_ENTITY);

    return criteria.list();
}