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:getdoctor.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods.//from w  w w.j  a  v a 2  s .  com
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");

    PrintWriter out = response.getWriter();
    try {

        SessionFactory sf = HibernateUtil.getSessionFactory();
        Session ss = sf.openSession();
        Transaction tr = ss.beginTransaction();
        String code = "";

        Criteria cr = ss.createCriteria(Doctor.class);

        ArrayList<Doctor> all = (ArrayList<Doctor>) cr.list();

        Criteria cr1 = ss.createCriteria(Doctor.class);
        cr1.add(Restrictions.eq("status", "ACTIVE"));
        if (request.getParameter("sort") != null) {
            String so = request.getParameter("sort");
            if (so.equals("1")) {
                cr1.addOrder(Order.asc("dFirstname"));
            } else if (so.equals("2")) {
                cr1.addOrder(Order.desc("dFirstname"));
            }
            //.add( Restrictions.like("name", "F%")
            //.addOrder( Order.asc("name") )
            //.addOrder( Order.desc("age") )
            //.setMaxResults(50)

            //                cr.add(Restrictions.eq("password", request.getParameter("password")));
        }
        if (request.getParameter("search") != null) {
            String so = request.getParameter("search");
            cr1.add(Restrictions.like("dFirstname", so + "%"));
        }
        ArrayList<Doctor> all1 = (ArrayList<Doctor>) cr1.list();
        if (all1.size() > 0) {
            for (int i = 0; i < all1.size(); i++) {
                Doctor dd = all1.get(i);
                out.println("<li class=\"author-comments\" style=\"margin-top: 36px\">\n"
                        + "                                <div class=\"media\">\n"
                        + "                                    <div class=\"media-left\">    \n"
                        + "                                        <img alt=\"img\" src=\"images/doctor-2.jpg\" class=\"media-object news-img\">      \n"
                        + "                                    </div>\n"
                        + "                                    <div class=\"media-body\">\n"
                        + "                                        <h4 class=\"author-name\">Dr. "
                        + dd.getDFirstname() + " " + dd.getDLastname() + "</h4>\n"
                        + "                                        <span class=\"comments-date\"> "
                        + dd.getQualificationId().getQualificationName() + "</span>\n"
                        + "                                        <p>"
                        + dd.getAddressid().getCityId().getCityName().toString() + "</p>\n"
                        + "                                        <p class=\"fa fa-check-circle\" style=\"color: green\">"
                        + dd.getStatus() + "</p>\n"
                        + "                                        <div class=\"ui heart rating\" data-rating=\"1\" data-max-rating=\"3\"></div>\n"
                        + "                                        <a class=\"reply-btn\" href=\"take_doc_app?docid="
                        + dd.getDId() + "\">Take Appointment</a>\n"
                        + "                                    </div>\n"
                        + "                                </div>\n" + "                            </li>");
            }
        }
        //   request.setAttribute("dlist", all);

    } catch (HibernateException he) {

        he.getMessage();

    } finally {

        out.close();
    }
}

From source file:GetGroupChatServ.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods.//w  ww .  ja v a 2s  .  co m
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();

    try {

        SessionFactory sf = NewHibernateUtil.getSessionFactory();
        Session ss = sf.openSession();
        Transaction tr = ss.beginTransaction();

        int gid = Integer.parseInt(request.getParameter("gid"));

        GroupDetail gd = (GroupDetail) ss.get(GroupDetail.class, gid);

        System.out.println("Group id is :" + gd.getGId());

        Criteria cr = ss.createCriteria(ChatMsg.class);
        cr.add(Restrictions.eq("gId", gd));
        cr.addOrder(Order.desc("cmid"));
        cr.setMaxResults(25);
        ArrayList<ChatMsg> cm = (ArrayList<ChatMsg>) cr.list();
        Collections.reverse(cm);

        System.out.println("size of the msg array :" + cm.size());
        request.setAttribute("msgs", cm);
        request.setAttribute("gobj", gd);

        RequestDispatcher rd = request.getRequestDispatcher("groupchat.jsp");
        rd.forward(request, response);

    } catch (HibernateException he) {
        System.out.print(he.getMessage());
    }
}

From source file:ViewPropertyServ.java

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();
    try {//from w w w.  j  a v  a 2s . c  om
        SessionFactory sf = util.NewHibernateUtil.getSessionFactory();
        Session ss = sf.openSession();

        Criteria cr = ss.createCriteria(PropFeedback.class);
        cr.addOrder(Order.desc("pfId"));
        List<PropFeedback> pfL = cr.list();

        request.setAttribute("pfL", pfL);

    } catch (HibernateException e) {
        out.println(e.getMessage());
    }
}

From source file:get_lab_admin.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods.//from  ww  w.j a  v a2  s .com
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");

    PrintWriter out = response.getWriter();

    try {

        SessionFactory sf = HibernateUtil.getSessionFactory();
        Session ss = sf.openSession();
        Transaction tr = ss.beginTransaction();
        String code = "";

        Criteria cr = ss.createCriteria(Lab.class);
        ArrayList<Lab> all = (ArrayList<Lab>) cr.list();

        Criteria cr1 = ss.createCriteria(Lab.class);
        cr1.add(Restrictions.eq("status", "ACTIVE"));
        if (request.getParameter("sort") != null) {
            String so = request.getParameter("sort");
            if (so.equals("1")) {
                cr1.addOrder(Order.asc("labName"));
            } else if (so.equals("2")) {
                cr1.addOrder(Order.desc("labName"));
            }
            //.add( Restrictions.like("name", "F%")
            //.addOrder( Order.asc("name") )
            //.addOrder( Order.desc("age") )
            //.setMaxResults(50)

            //                cr.add(Restrictions.eq("password", request.getParameter("password")));
        }
        if (request.getParameter("search") != null) {
            String so = request.getParameter("search");
            cr1.add(Restrictions.like("labName", so + "%"));
        }
        ArrayList<Lab> all1 = (ArrayList<Lab>) cr1.list();
        if (all1.size() > 0) {
            for (int i = 0; i < all1.size(); i++) {
                Lab dd = all1.get(i);
                out.println("<li class=\"author-comments\" style=\"margin-top: 36px\">\n"
                        + "                                <div class=\"media\">\n"
                        + "                                    <div class=\"media-left\">    \n"
                        + "                                        <img alt=\"img\" src=\"images/Lab-Icon-245x300.png\" class=\"media-object news-img\">      \n"
                        + "                                    </div>\n"
                        + "                                    <div class=\"media-body\">\n"
                        + "                                        <h4 class=\"author-name\">" + dd.getLabName()
                        + "</h4>\n" + "                                        <a target='_blank' href='http://"
                        + dd.getWebsite() + "'> <span class=\"comments-date\">" + dd.getWebsite()
                        + "</span> </a>\n" + "                                        <p>"
                        + dd.getAddressid().getCityId().getCityName() + "</p>\n"
                        + "                                        <p>" + dd.getStatus() + "</p>\n"
                        + "                                        <div class=\"ui heart rating\" data-rating=\"1\" data-max-rating=\"3\"></div>\n"
                        + "                                        <a class=\"reply-btn\" href=\"view_labprofile_admin?lid="
                        + dd.getLId() + "\">View Profile</a>\n" + "                                    </div>\n"
                        + "                                </div>\n" + "                            </li>");
            }

        }
        request.setAttribute("dlist", all);

    }

    catch (HibernateException he) {
        out.println(he.getMessage());
    } finally {
        out.close();
    }
}

From source file:get_doctor_admin.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods./*from  w  w w.  jav  a  2  s.  co m*/
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");

    PrintWriter out = response.getWriter();
    try {

        SessionFactory sf = HibernateUtil.getSessionFactory();
        Session ss = sf.openSession();
        Transaction tr = ss.beginTransaction();
        String code = "";

        Criteria cr = ss.createCriteria(Doctor.class);

        ArrayList<Doctor> all = (ArrayList<Doctor>) cr.list();

        Criteria cr1 = ss.createCriteria(Doctor.class);
        cr1.add(Restrictions.eq("status", "ACTIVE"));
        if (request.getParameter("sort") != null) {
            String so = request.getParameter("sort");
            if (so.equals("1")) {
                cr1.addOrder(Order.asc("dFirstname"));
            } else if (so.equals("2")) {
                cr1.addOrder(Order.desc("dFirstname"));
            }
            //.add( Restrictions.like("name", "F%")
            //.addOrder( Order.asc("name") )
            //.addOrder( Order.desc("age") )
            //.setMaxResults(50)

            //                cr.add(Restrictions.eq("password", request.getParameter("password")));
        }
        if (request.getParameter("search") != null) {
            String so = request.getParameter("search");
            cr1.add(Restrictions.like("dFirstname", so + "%"));
        }
        ArrayList<Doctor> all1 = (ArrayList<Doctor>) cr1.list();
        if (all1.size() > 0) {
            for (int i = 0; i < all1.size(); i++) {
                Doctor dd = all1.get(i);
                out.println("<li class=\"author-comments\" style=\"margin-top: 36px\">\n"
                        + "                                <div class=\"media\">\n"
                        + "                                    <div class=\"media-left\">    \n"
                        + "                                        <img alt=\"img\" src=\"images/doctor-2.jpg\" class=\"media-object news-img\">      \n"
                        + "                                    </div>\n"
                        + "                                    <div class=\"media-body\">\n"
                        + "                                        <h4 class=\"author-name\">Dr. "
                        + dd.getDFirstname() + " " + dd.getDLastname() + "</h4>\n"
                        + "                                        <span class=\"comments-date\"> "
                        + dd.getQualificationId().getQualificationName() + "</span>\n"
                        + "                                        <p>"
                        + dd.getAddressid().getCityId().getCityName().toString() + "</p>\n"
                        + "                                        <p class=\"fa fa-check-circle\" style=\"color: green\">"
                        + dd.getStatus() + "</p>\n"
                        + "                                        <div class=\"ui heart rating\" data-rating=\"1\" data-max-rating=\"3\"></div>\n"
                        + "                                        <a class=\"reply-btn\" href=\"view_docprofile_admin?did="
                        + dd.getDId() + "\">View profile</a>\n" + "                                    </div>\n"
                        + "                                </div>\n" + "                            </li>");
            }
        }
        request.setAttribute("dlist", all);

    }

    catch (HibernateException he) {
        out.println(he.getMessage());
    }

    finally {
        out.close();
    }
}

From source file:getlab.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods.//ww w . j  a  v  a  2 s. c o  m
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");

    PrintWriter out = response.getWriter();
    try {

        SessionFactory sf = HibernateUtil.getSessionFactory();
        Session ss = sf.openSession();
        Transaction tr = ss.beginTransaction();
        String code = "";

        Criteria cr = ss.createCriteria(Lab.class);
        ArrayList<Lab> all = (ArrayList<Lab>) cr.list();
        //            request.setAttribute("dlist", all);
        Criteria cr1 = ss.createCriteria(Lab.class);
        cr1.add(Restrictions.eq("status", "ACTIVE"));
        if (request.getParameter("sort") != null) {
            String so = request.getParameter("sort");
            if (so.equals("1")) {
                cr1.addOrder(Order.asc("labName"));
            } else if (so.equals("2")) {
                cr1.addOrder(Order.desc("labName"));
            }
            //.add( Restrictions.like("name", "F%")
            //.addOrder( Order.asc("name") )
            //.addOrder( Order.desc("age") )
            //.setMaxResults(50)

            //                cr.add(Restrictions.eq("password", request.getParameter("password")));
        }
        if (request.getParameter("search") != null) {
            String so = request.getParameter("search");
            cr1.add(Restrictions.like("labName", so + "%"));
        }
        ArrayList<Lab> all1 = (ArrayList<Lab>) cr1.list();
        if (all1.size() > 0) {
            for (int i = 0; i < all1.size(); i++) {
                Lab dd = all1.get(i);
                out.println("<li class=\"author-comments\" style=\"margin-top: 36px\">\n"
                        + "                                <div class=\"media\">\n"
                        + "                                    <div class=\"media-left\">    \n"
                        + "                                        <img alt=\"img\" src=\"images/Lab-Icon-245x300.png\" class=\"media-object news-img\">      \n"
                        + "                                    </div>\n"
                        + "                                    <div class=\"media-body\">\n"
                        + "                                        <h4 class=\"author-name\">" + dd.getLabName()
                        + "</h4>\n" + "                                        <a target='_blank' href='http://"
                        + dd.getWebsite() + "'> <span class=\"comments-date\">" + dd.getWebsite()
                        + "</span> </a>\n" + "                                        <p>"
                        + dd.getAddressid().getCityId().getCityName() + "</p>\n"
                        + "                                        <p class=\"fa fa-check-circle\" style=\"color: green\">"
                        + dd.getStatus() + "</p>\n"
                        + "                                        <div class=\"ui heart rating\" data-rating=\"1\" data-max-rating=\"3\"></div>\n"
                        + "                                        <a class=\"reply-btn\" href=\"take_test?labid="
                        + dd.getLId() + "\">Take Appointment</a>\n"
                        + "                                    </div>\n"
                        + "                                </div>\n" + "                            </li>");
            }

        }
    } catch (HibernateException he) {
        he.getMessage();
    } finally {
        out.close();
    }
}

From source file:abid.password.swing.dao.hibernate.AbstractHibernateDao.java

License:Apache License

@SuppressWarnings("unchecked")
public List<T> getSorted(int first, int fetchSize, String propertyName, boolean ascending) {
    Session session = sessionFactory.openSession();
    try {//w  w w .  j  a va  2 s . c o  m
        Criteria criteria = session.createCriteria(persistentClass);
        criteria.setFirstResult(first);
        criteria.setMaxResults(fetchSize);
        criteria.setFetchSize(fetchSize);
        criteria.setCacheable(true);

        if (ascending) {
            criteria.addOrder(Order.asc(propertyName));
        } else {
            criteria.addOrder(Order.desc(propertyName));
        }

        List<T> dataList = (List<T>) criteria.list();
        return dataList;
    } finally {
        session.close();
    }
}

From source file:abid.password.wicket.dao.AbstractHibernateDao.java

License:Apache License

@Transactional(type = TransactionType.READ_ONLY)
@SuppressWarnings("unchecked")
public List<T> getSorted(int first, int fetchSize, String propertyName, boolean ascending) {
    Criteria criteria = session.get().createCriteria(persistentClass);
    criteria.setFirstResult(first);//from  w w  w .j  a va  2 s . c  o m
    criteria.setMaxResults(fetchSize);
    criteria.setFetchSize(fetchSize);
    criteria.setCacheable(true);

    if (ascending) {
        criteria.addOrder(Order.asc(propertyName));
    } else {
        criteria.addOrder(Order.desc(propertyName));
    }

    List<T> dataList = (List<T>) criteria.list();
    return dataList;
}

From source file:apm.common.core.DaoImpl.java

License:Open Source License

/**
 * /*from   www .j  a  v a2 s .  c o  m*/
 * @param page
 * @param detachedCriteria
 * @param resultTransformer
 * @return
 */
@SuppressWarnings("unchecked")
public Page<T> find(Page<T> page, DetachedCriteria detachedCriteria, ResultTransformer resultTransformer) {
    // get count
    if (!page.isDisabled() && !page.isNotCount()) {
        page.setCount(count(detachedCriteria));
        if (page.getCount() < 1) {
            return page;
        }
    }
    Criteria criteria = detachedCriteria.getExecutableCriteria(getSession());
    criteria.setResultTransformer(resultTransformer);
    // 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:ar.edu.unju.fi.apu.dao.imp.mysql.PropietarioDAOImp.java

@Override
public List<Propietario> obtenerTodos() {
    Session session = HibernateUtil.getSessionFactory().openSession();
    Criteria criteria = session.createCriteria(Propietario.class);
    criteria.add(Restrictions.eq("estado", true));
    criteria.addOrder(Order.asc("apellido"));
    List propietarios = criteria.list();
    return propietarios;
}