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.abssh.util.GenericDao.java

License:Apache License

/**
 * countCriteria.//from   w  w w  . ja  va 2  s.com
 */
@SuppressWarnings("unchecked")
protected int sumCriteriaResult(final String propertyName, final Criteria c) {
    CriteriaImpl impl = (CriteriaImpl) c;

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

    // Sum
    Integer sum = (Integer) c.setProjection(Projections.sum(propertyName)).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("can not throw Exception:{}", e.getMessage());
    }

    return sum == null ? 0 : sum;
}

From source file:com.abssh.util.GenericDao.java

License:Apache License

public List<T> findDynamicFetch(final List<PropertyFilter> filters, String orderBy, String order,
        final String... lazyObjects) {
    Criteria criteria = getSession().createCriteria(entityClass);
    Map<String, Criteria> criteriaMap = new HashMap<String, Criteria>();
    for (PropertyFilter filter : filters) {
        if (!filter.isMultiProperty()) {
            String propertyName = filter.getPropertyName();
            Object[] propertyValue = filter.getPropertyValue();
            MatchType matchType = filter.getMatchType();
            Criteria parent = findParentCriteria(criteria, propertyName, criteriaMap);
            String[] tmp = StringUtils.split(propertyName, DEF_SEPARATOR);
            parent.add(getCriterion(tmp[tmp.length - 1], propertyValue, matchType));
        } else {//from  www .j a va  2  s  .  c o m
            Disjunction disjunction = Restrictions.disjunction();
            Object[] propertyValue = filter.getPropertyValue();
            MatchType matchType = filter.getMatchType();
            for (String propertyName : filter.getPropertyNames()) {
                Criteria parent = findParentCriteria(criteria, propertyName, criteriaMap);
                String[] tmp = StringUtils.split(propertyName, DEF_SEPARATOR);
                parent.add(getCriterion(tmp[tmp.length - 1], propertyValue, matchType));

            }
            criteria.add(disjunction);
        }
    }

    if (orderBy != null && !"".equals(orderBy)) {
        String[] orderByArray = StringUtils.split(orderBy, ',');
        String[] orderArray = StringUtils.split(order, ',');

        Assert.isTrue(orderByArray.length == orderArray.length, "orderBy and order is not suited!");

        for (int i = 0; i < orderByArray.length; i++) {
            if (orderByArray[i].indexOf(".") > 0) {
                // ???
                if (Page.ASC.equals(orderArray[i])) {
                    Criteria p = criteriaMap
                            .get(orderByArray[i].substring(0, orderByArray[i].lastIndexOf(".")));
                    if (p == null) {
                        p = findParentCriteria(criteria, orderByArray[i], criteriaMap);// ??
                    }
                    p.addOrder(Order.asc(orderByArray[i].substring(orderByArray[i].lastIndexOf(".") + 1)));
                } else {
                    Criteria p = criteriaMap
                            .get(orderByArray[i].substring(0, orderByArray[i].lastIndexOf(".")));
                    if (p == null) {
                        p = findParentCriteria(criteria, orderByArray[i], criteriaMap);// ??
                    }
                    p.addOrder(Order.desc(orderByArray[i].substring(orderByArray[i].lastIndexOf(".") + 1)));
                }
            } else {
                if (Page.ASC.equals(orderArray[i])) {
                    criteria.addOrder(Order.asc(orderByArray[i]));
                } else {
                    criteria.addOrder(Order.desc(orderByArray[i]));
                }
            }
        }
    }

    if (lazyObjects != null) {
        for (int i = 0; i < lazyObjects.length; i++) {
            criteria.setFetchMode(lazyObjects[i], FetchMode.JOIN);
        }
    }
    criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
    List result = criteria.list();

    return result;
}

From source file:com.aistor.common.persistence.BaseDaoImpl.java

License:Open Source License

/**
 * /*from w ww  .  j  a  v  a2  s  .c  om*/
 * @param detachedCriteria
 * @param page
 * @return
 */
@SuppressWarnings("unchecked")
public Page<T> find(Page<T> page, DetachedCriteria detachedCriteria) {
    // get count
    if (!page.isDisabled() && !page.isNotCount()) {
        page.setCount(count(detachedCriteria));
        if (page.getCount() < 1) {
            return page;
        }
    }
    Criteria criteria = detachedCriteria.getExecutableCriteria(getSession());
    criteria.setResultTransformer(Criteria.ROOT_ENTITY);
    // set page
    if (!page.isDisabled()) {
        criteria.setFirstResult(page.getFirstResult());
        criteria.setMaxResults(page.getMaxResults());
    }
    // order by
    if (StringUtils.isNotBlank(page.getOrderBy())) {
        for (String order : StringUtils.split(page.getOrderBy(), ",")) {
            String[] o = StringUtils.split(order, " ");
            if (o.length == 1) {
                criteria.addOrder(Order.asc(o[0]));
            } else if (o.length == 2) {
                if ("DESC".equals(o[1].toUpperCase())) {
                    criteria.addOrder(Order.desc(o[0]));
                } else {
                    criteria.addOrder(Order.asc(o[0]));
                }
            }
        }
    }
    page.setList(criteria.list());
    return page;
}

From source file:com.aistor.common.persistence.BaseDaoImpl.java

License:Open Source License

/**
 * //from   w  w w .  ja  va2  s  .c  o m
 * @param detachedCriteria
 * @return
 */
@SuppressWarnings("unchecked")
public List<T> find(DetachedCriteria detachedCriteria) {
    Criteria criteria = detachedCriteria.getExecutableCriteria(getSession());
    criteria.setResultTransformer(Criteria.ROOT_ENTITY);
    return criteria.list();
}

From source file:com.algoTrader.CriteriaSearch.java

/**
 * Locates a <code>Criteria</code> for a <code>childEntityName</code>. If a
 * <code>Criteria</code> exists for the <code>childEntityName</code>, it is returned. If
 * not, one is created and referenced in the <code>childCriteriaMap</code> under the
 * <code>childEntityName</code>.
 *
 * @param childEntityName/*  w w w.j a  v a  2 s.c o  m*/
 * @param parentCriteria
 * @return criteria The Criteria for the childEntityName.
 * @throws HibernateException
 */
private Criteria locateCriteria(String childEntityName, Criteria parentCriteria) throws HibernateException {
    if (this.childCriteriaMap.containsKey(childEntityName)) {
        return (Criteria) this.childCriteriaMap.get(childEntityName);
    }
    Criteria childCriteria = parentCriteria.createCriteria(childEntityName);
    if (this.configuration.isForceEagerLoading()) {
        parentCriteria.setFetchMode(childEntityName, FetchMode.JOIN);
    }

    // Hibernate does not support a 'unique' identifier. As a search may contain outer joins,
    // duplicates in the resultList are possible. We eliminate any duplicates here, creating a
    // distinctified resultSet (Suggestion from Hibernate itself; see www.hibernate.org's FAQ's).
    parentCriteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
    this.childCriteriaMap.put(childEntityName, childCriteria);
    return childCriteria;
}

From source file:com.all.client.model.LocalModelDao.java

License:Apache License

@SuppressWarnings("unchecked")
public List<Playlist> findAllUntitled() {
    List<Playlist> playlists = (List<Playlist>) hibernateTemplate.executeFind(new HibernateCallback() {
        public Object doInHibernate(Session sess) throws SQLException {
            Criteria crit = sess.createCriteria(LocalPlaylist.class);
            crit.add(Restrictions.ilike("name", "%Untitled Playlist%"));
            crit.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
            return crit.list();
        }// w  ww.  j a  v a  2  s  .  c  o m
    });
    return playlists;
}

From source file:com.app.billingapp.dao.impl.CategotyImpl.java

@Override
public List<Categories> findAllCategories() {
    List<Categories> categories = null;
    Criteria crit = createEntityCriteria();
    crit.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
    categories = crit.list();/*  w w  w. ja v a  2 s . c  om*/
    return categories;
}

From source file:com.app.billingapp.dao.impl.OrderDaoImpl.java

@Override
public List<OrderMaster> findAll() {
    List<OrderMaster> orderMasters = null;
    Criteria criteria = createEntityCriteria();
    criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
    orderMasters = criteria.list();/*from  w  w  w .  j  ava 2  s.co m*/
    return orderMasters;
}

From source file:com.app.billingapp.dao.impl.ProductDaoImpl.java

@Override
public List<Products> findAll() {
    List<Products> products = null;
    Criteria crit = createEntityCriteria();
    crit.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
    products = crit.list();/*  w  w w  . j  a v a 2  s.  c  o  m*/
    return products;
}

From source file:com.app.billingapp.dao.impl.PurchaseDaoImpl.java

@Override
public List<PurchaseMaster> findAll() {
    List<PurchaseMaster> purchaseMasters = null;
    Criteria criteria = createEntityCriteria();
    criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
    purchaseMasters = criteria.list();//  w w  w. j a v  a 2 s . c o m
    return purchaseMasters;
}