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:br.com.itw.commons.persistence.CoreRepository.java

License:Apache License

public Page<T> search(T entity, Pageable pageable) {
    if (pageable == null) {
        pageable = new PageRequest(0, 10);
    }//from w  w  w . j av a2 s .c o  m

    Session session = (Session) entityManager.getDelegate();
    Criteria criteria = session.createCriteria(entity.getClass());
    // Prepare Example
    Example example = Example.create(entity);
    criteria.add(example.enableLike(MatchMode.ANYWHERE).ignoreCase());

    // Count
    Long totalItems = (Long) criteria.setProjection(Projections.rowCount()).uniqueResult();

    // Pageable result Result
    criteria.setProjection(null).setResultTransformer(CriteriaSpecification.ROOT_ENTITY);
    List<T> result = criteria.setFirstResult((pageable.getPageNumber() - 1) * pageable.getPageSize())
            .setMaxResults(pageable.getPageSize()).list();
    return (Page<T>) new PageImpl<T>(result, pageable, totalItems);
}

From source file:br.com.itw.commons.persistence.PageableHelper.java

License:Apache License

public static Page<?> getPage(Criteria criteria, Pageable pageable) {
    // Count/*w  w w  .  j  av  a  2s.c  o  m*/
    Long totalItems = (Long) criteria.setProjection(Projections.rowCount()).uniqueResult();

    // Pageable result Result
    criteria.setProjection(null).setResultTransformer(CriteriaSpecification.ROOT_ENTITY);
    if (pageable.getSort() != null) {
        Iterator<Sort.Order> orders = pageable.getSort().iterator();
        while (orders.hasNext()) {
            Sort.Order order = orders.next();
            if (order.getDirection() == null || Sort.Direction.ASC.equals(order.getDirection())) {
                criteria.addOrder(Order.asc(order.getProperty()));
            } else {
                criteria.addOrder(Order.desc(order.getProperty()));
            }
        }
    }
    List<?> result = criteria.setFirstResult(pageable.getPageNumber() * pageable.getPageSize())
            .setMaxResults(pageable.getPageSize()).list();
    return (Page<?>) new PageImpl<>(result, pageable, totalItems);
}

From source file:cn.hxh.springside.orm.hibernate.HibernateDao.java

License:Apache License

/**
 * countCriteria./*from w  ww.j av a  2  s.c  o  m*/
 */
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:cn.newtouch.util.hibernate.HibernateDao.java

License:Apache License

/**
 * countCriteria.//from   w ww . ja va2  s  .  c  o m
 */
@SuppressWarnings("unchecked")
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.abssh.util.GenericDao.java

License:Apache License

/**
 * countCriteria.//from w w  w  .  j av a 2s  . c o m
 */
@SuppressWarnings("unchecked")
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("can not throw Exception:{}", e.getMessage());
    }

    // Count
    Long ct = (Long) c.setProjection(Projections.rowCount()).uniqueResult();
    int totalCount = ct == null ? 0 : ct.intValue();

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

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

License:Apache License

/**
 * countCriteria./* ww w.  j a  va 2 s.  c  o  m*/
 */
@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.dosport.hibernate.utils.HibernateDao.java

License:Apache License

/**
 * countCriteria.//  w w w  .j  ava2  s.c  om
 */
@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.eryansky.common.orm.core.hibernate.support.HibernateSupportDao.java

License:Apache License

/**
 * countCriteria.//from  w  ww .  j av a2s  .  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.hyzy.core.orm.hibernate.HibernateDao.java

License:Apache License

/**
 * countCriteria./*from www.  j  a  v a2s .com*/
 */
@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 = 0l;
    try {
        totalCountObject = (Long) c.setProjection(Projections.rowCount()).uniqueResult();
    } catch (Exception e) {
        e.printStackTrace();
    }
    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.lakeside.data.sqldb.PageBaseDao.java

License:Apache License

/**
 * countCriteria.//from   ww  w  . java 2  s.  c  o m
 */
@SuppressWarnings("unchecked")
protected int countCriteriaResult(final Criteria c) {
    CriteriaImpl impl = (CriteriaImpl) c;

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

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

    return totalCount;
}