Example usage for org.hibernate Criteria addOrder

List of usage examples for org.hibernate Criteria addOrder

Introduction

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

Prototype

public Criteria addOrder(Order order);

Source Link

Document

Add an Order ordering to the result set.

Usage

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 {/*w ww.  j a v a 2s . co 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.actop.model.ApprovalManagement.java

public List<PaymentApproval> checkPayementApproval(DepartmentsHasDesignation dhd, boolean checkOnly) {
    Session s = Connection.getSessionFactory().openSession();
    Criteria c = s.createCriteria(PaymentApproval.class);
    c.add(Restrictions.eq("departmentsHasDesignation", dhd));
    if (checkOnly) {
        c.add(Restrictions.eq("status", 0));
    }//from  ww  w. j a v  a 2s .c  o  m
    c.addOrder(Order.desc("departmentsHasDesignationHasPaymentsId"));
    List<PaymentApproval> approvals = c.list();
    s.close();
    return approvals;
}

From source file:com.actop.model.ApprovalManagement.java

public List<LeaveApproval> checkLeaveApproval(DepartmentsHasDesignation dhd, boolean checkOnly) {
    Session s = Connection.getSessionFactory().openSession();
    Criteria c = s.createCriteria(LeaveApproval.class);
    c.add(Restrictions.eq("departmentsHasDesignation", dhd));
    if (checkOnly) {
        c.add(Restrictions.eq("status", 0));
    }/*from w w  w  .j a  v  a  2s. c  om*/
    c.addOrder(Order.desc("leaveHasDepartmentsHasDesignationId"));
    List<LeaveApproval> approvals = c.list();
    s.close();
    return approvals;
}

From source file:com.actop.model.ApprovalManagement.java

public List<ProjectsApproval> checkProjectsApproval(DepartmentsHasDesignation dhd, boolean checkOnly) {
    Session s = Connection.getSessionFactory().openSession();
    Criteria c = s.createCriteria(ProjectsApproval.class);
    c.add(Restrictions.eq("departmentsHasDesignation", dhd));
    if (checkOnly) {
        c.add(Restrictions.eq("status", 0));
    }//from  w ww . j  ava2 s.co m
    c.addOrder(Order.desc("projectsHasDepartmentsHasDesignationId"));
    List<ProjectsApproval> approvals = c.list();
    s.close();
    return approvals;
}

From source file:com.actop.model.ApprovalManagement.java

public List<PromotionApproval> checkPromotionApproval(DepartmentsHasDesignation dhd, boolean checkOnly) {
    Session s = Connection.getSessionFactory().openSession();
    Criteria c = s.createCriteria(PromotionApproval.class);
    c.add(Restrictions.eq("departmentsHasDesignation", dhd));
    if (checkOnly) {
        c.add(Restrictions.eq("status", 0));
    }/*  ww w  . j  a  v a2 s .c o m*/
    c.addOrder(Order.desc("promotionsHasDepartmentsHasDesignationId"));
    List<PromotionApproval> approvals = c.list();
    s.close();
    return approvals;
}

From source file:com.actop.model.ApprovalManagement.java

public List<ProjectTasksApproval> checkProjectTaskApproval(DepartmentsHasDesignation dhd, boolean checkOnly) {
    Session s = Connection.getSessionFactory().openSession();
    Criteria c = s.createCriteria(ProjectTasksApproval.class);
    c.add(Restrictions.eq("departmentsHasDesignation", dhd));
    if (checkOnly) {
        c.add(Restrictions.eq("status", 0));
    }//from   ww  w .j a  v a 2  s  . c  o  m
    c.addOrder(Order.desc("projectTasksHasDepartmentsHasDesignationId"));
    List<ProjectTasksApproval> approvals = c.list();
    s.close();
    return approvals;
}

From source file:com.actop.model.ApprovalManagement.java

public List<OtherApprovals> checkOtherApproval(DepartmentsHasDesignation dhd, boolean checkOnly) {
    Session s = Connection.getSessionFactory().openSession();
    Criteria c = s.createCriteria(OtherApprovals.class);
    c.add(Restrictions.eq("departmentsHasDesignation", dhd));
    if (checkOnly) {
        c.add(Restrictions.eq("status", 0));
    }//from   ww w .j a  va2 s.  c  om
    c.addOrder(Order.desc("otherHasDepartmentsHasDesignationId"));
    List<OtherApprovals> approvals = c.list();
    s.close();
    return approvals;
}

From source file:com.actop.model.ApprovalManagement.java

public List<AllowanceApproval> checkAllowanceApproval(DepartmentsHasDesignation dhd, boolean checkOnly) {
    Session s = Connection.getSessionFactory().openSession();
    Criteria c = s.createCriteria(AllowanceApproval.class);
    c.add(Restrictions.eq("departmentsHasDesignation", dhd));
    if (checkOnly) {
        c.add(Restrictions.eq("status", 0));
    }//from w  ww. j a  v a  2 s  . c o m
    c.addOrder(Order.desc("employersHasAllowancesHasDepartmentsHasDesignationId"));
    List<AllowanceApproval> approvals = c.list();
    s.close();
    return approvals;
}

From source file:com.actop.model.AttendanceManagement.java

public List<Attendance> getAllAttendance() {
    Session s = Connection.getSessionFactory().openSession();
    Criteria c = s.createCriteria(Attendance.class);
    c.addOrder(Order.desc("dateTime"));
    List<Attendance> attendances = c.list();
    s.flush();/*  w ww  .ja  v  a2s  .  c  o m*/
    s.close();
    return attendances;
}

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

License:Open Source License

/**
 * //w  w  w  .j a  v  a 2  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;
}