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.thinkgem.jeesite.common.persistence.BaseDaoImpl.java

License:Open Source License

/**
 * // w  ww. j a  v  a 2 s  .c  o m
 * 
 * @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.thinkmore.framework.orm.hibernate.HibernateDao.java

License:Apache License

/**
 * countCriteria.//w w  w. ja v a 2  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) ReflectionUtil.getFieldValue(impl, "orderEntries");
        ReflectionUtil.setFieldValue(impl, "orderEntries", new ArrayList());
    } catch (Exception e) {
        logger.error("??:{}", e.getMessage());
    }
    // 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 {
        ReflectionUtil.setFieldValue(impl, "orderEntries", orderEntries);
    } catch (Exception e) {
        logger.error("??:{}", e.getMessage());
    }
    return totalCount;
}

From source file:com.todos.springmvc.dao.TodoDaoImpl.java

@SuppressWarnings("unchecked")
public List<Todo> findAllTodos() {
    Criteria criteria = createEntityCriteria().addOrder(Order.asc("name"));
    criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
    List<Todo> users = (List<Todo>) criteria.list();

    return users;
}

From source file:com.trt.dao.GenericDaoImpl.java

@Override
public EntityType findEntityByCriteriaList(List<Criterion> criterions) {
    EntityType entityObj = null;/*from   ww w.ja  v a 2  s  . c  o m*/
    try {
        session = sessionFactory.getCurrentSession();
    } catch (Exception e) {
        session = sessionFactory.openSession();
    }
    try {
        tx = session.beginTransaction();
        Criteria criteria = session.createCriteria(persistentClass);
        for (Criterion c : criterions) {
            criteria.add(c);
        }
        criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
        entityObj = (EntityType) criteria.uniqueResult();
        tx.commit();
    } catch (HibernateException e) {
        if (tx != null) {
            tx.rollback();
        }
        e.printStackTrace();
    } finally {
        session.close();
    }
    return entityObj;
}

From source file:com.trt.dao.GenericDaoImpl.java

@Override
public List<EntityType> findByCriteriaList(List<Criterion> criterions) {
    List<EntityType> list = null;
    try {/*from ww  w. j ava2  s  .  com*/
        session = sessionFactory.getCurrentSession();
    } catch (Exception e) {
        session = sessionFactory.openSession();
    }
    try {
        tx = session.beginTransaction();
        Criteria criteria = session.createCriteria(persistentClass);
        for (Criterion c : criterions) {
            criteria.add(c);
        }
        criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
        list = criteria.list();
        tx.commit();
    } catch (HibernateException e) {
        if (tx != null) {
            tx.rollback();
        }
        e.printStackTrace();
    } finally {
        session.close();
    }
    return list;
}

From source file:com.tunisbank.dao.CompteDAO.java

License:GNU General Public License

@SuppressWarnings("unchecked")
public List<Compte> getAll() {
    Criteria criteria = sessionFactory.getCurrentSession().createCriteria(Compte.class);
    criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
    return criteria.list();
}

From source file:com.tunisbank.dao.GroupesDAO.java

License:GNU General Public License

@SuppressWarnings("unchecked")
public List<Groupe> getAll() {
    Criteria criteria = sessionFactory.getCurrentSession().createCriteria(Groupe.class);
    criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
    return criteria.list();
}

From source file:com.tunisbank.dao.SuivieDAO.java

License:GNU General Public License

@SuppressWarnings("unchecked")
public List<Suivie> getAll() {
    Criteria criteria = sessionFactory.getCurrentSession().createCriteria(Suivie.class);
    criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
    return criteria.list();
}

From source file:com.tunisbank.dao.UsersDAO.java

License:GNU General Public License

@SuppressWarnings("unchecked")
public List<User> getAll() {
    Criteria criteria = sessionFactory.getCurrentSession().createCriteria(User.class);
    criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
    return criteria.list();
}

From source file:com.tysanclan.site.projectewok.entities.dao.hibernate.ExpenseDAOImpl.java

License:Open Source License

@Override
protected Criteria createCriteria(SearchFilter<Expense> filter) {
    Criteria criteria = getSession().createCriteria(Expense.class);

    if (filter instanceof ExpenseFilter) {
        ExpenseFilter eFilter = (ExpenseFilter) filter;
        if (eFilter.getFrom() != null) {
            criteria.add(/*from  w  w  w .ja  v a2s  .c o m*/
                    Restrictions.or(Restrictions.ge("end", eFilter.getFrom()), Restrictions.isNull("end")));
        }
        if (eFilter.getTo() != null) {
            criteria.add(
                    Restrictions.or(Restrictions.le("start", eFilter.getTo()), Restrictions.isNull("start")));
        }
    }

    criteria.setResultTransformer(DistinctRootEntityResultTransformer.INSTANCE);

    return criteria;
}