Example usage for org.hibernate Criteria setProjection

List of usage examples for org.hibernate Criteria setProjection

Introduction

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

Prototype

public Criteria setProjection(Projection projection);

Source Link

Document

Used to specify that the query results will be a projection (scalar in nature).

Usage

From source file:com.certus.actions.customersLoadAction.java

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    Session ses = com.certus.connection.HibernateUtil.getSessionFactory().openSession();
    int pageIndex = 0;
    int totalNumberOfRecords = 0;
    int numberOfRecordsPerPage = 9;

    String sPageIndex = request.getParameter("pgIndex");
    pageIndex = sPageIndex == null ? 1 : Integer.parseInt(sPageIndex);

    int s = (pageIndex * numberOfRecordsPerPage) - numberOfRecordsPerPage;
    List<User> uList = ses.createCriteria(User.class).setFirstResult(s).setMaxResults(numberOfRecordsPerPage)
            .list();/*from   w w  w .j  a v  a2s  . c  o  m*/

    String s1 = "", s2 = "";
    for (User u : uList) {
        String avlOn = u.isAvailability() ? "btn-danger" : "btn-default";
        String avlOff = u.isAvailability() ? "btn-default" : "btn-danger";
        String orders = orderCount(u, ses) != 0 ? "<button title='view order' onclick='loadModal("
                + loadOrders(u, ses)
                + ");' class='btn btn-default' data-dismiss='modal' data-target='#ViewOrdersModal' data-toggle='modal'>"
                + orderCount(u, ses) + "</button>"
                : "<span style='margin-left:10%;'>" + orderCount(u, ses) + "</span>";
        s1 += "<tr>" + "<td>" + u.getFName() + " " + u.getLName() + "</td>" + "<td>"
                + "<div class='btn-group btn-toggle'>" + "<button class='btn btn-xs " + avlOn
                + "' onclick='onBtnClick(" + u.getId() + "," + pageIndex + ");' type='button'>ON</button>"
                + "<button class='btn btn-xs " + avlOff + "' onclick='offBtnClick(" + u.getId() + ","
                + pageIndex + ");' type='button'>OFF</button>" + "</div>" + "</td>" + "<td>" + u.getEmail()
                + "</td>" + "<td>" + orders + "</td>" + "<td>" + "<a href='edit_customer.jsp?uid=" + u.getId()
                + "'>" + "    <span class='glyphicon glyphicon-edit'></span>" + "</a>" + "</td>" + "</tr>"
                + "<tr> ";
    }

    Criteria criteriaCount = ses.createCriteria(User.class);
    criteriaCount.setProjection(Projections.rowCount());
    totalNumberOfRecords = (int) (long) (Long) criteriaCount.uniqueResult();

    int noOfPages = totalNumberOfRecords / numberOfRecordsPerPage;

    if (totalNumberOfRecords > (noOfPages * numberOfRecordsPerPage)) {
        noOfPages = noOfPages + 1;
    }

    for (int j = 1; j <= noOfPages; j++) {
        if (noOfPages > 1) {
            String myurl = "customers.jsp?pgIndex=" + j;
            if (j == pageIndex) {
                s2 += "<span class='current'>" + j + "</span>";
            } else {
                s2 += "<a href='" + myurl + "'>" + j + "</a>";
            }

        }
    }
    ses.close();

    response.setContentType("application/json");
    response.setCharacterEncoding("UTF-8");
    response.getWriter().write("[{\"d1\":\"" + s1 + "\",\"d2\":\"" + s2 + "\"}]");

}

From source file:com.certus.actions.loadBrandsAction.java

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    Session session = com.certus.connection.HibernateUtil.getSessionFactory().openSession();
    int pageIndex = 0;
    int totalNumberOfRecords = 0;
    int numberOfRecordsPerPage = 9;

    String sPageIndex = request.getParameter("pgIndex");
    pageIndex = sPageIndex == null ? 1 : Integer.parseInt(sPageIndex);

    int s = (pageIndex * numberOfRecordsPerPage) - numberOfRecordsPerPage;
    List<Brand> b = session.createCriteria(Brand.class).setFirstResult(s).setMaxResults(numberOfRecordsPerPage)
            .list();//from w  ww.  j  ava 2 s  . c o m
    String s1 = "", s2 = "";
    for (Brand brnd : b) {
        String avlOn = brnd.isAvailability() ? "btn-danger" : "btn-default";
        String avlOff = brnd.isAvailability() ? "btn-default" : "btn-danger";
        s1 += "<tr>" + "<td>" + brnd.getBrandName() + "</td>" + "<td>" + "<div class='btn-group btn-toggle'>"
                + "<button class='btn btn-xs " + avlOn + "' onclick='onBtnClick(" + brnd.getId() + ","
                + pageIndex + ");' type='button'>ON</button>" + "<button class='btn btn-xs " + avlOff
                + "' onclick='offBtnClick(" + brnd.getId() + "," + pageIndex + ");' type='button'>OFF</button>"
                + "</div>" + "</td>" + "<td>"
                + "<button ype='button' class='btn btn-default' data-dismiss='modal' onclick='getBrandId("
                + brnd.getId() + ");' data-target='#ViewAllProModal' data-toggle='modal'>"
                + "<span class='glyphicon glyphicon-gift'></span>&nbsp;&nbsp;"
                + brnd.getProducts().stream().count() + "</button>" + "</td>" + "<td>"
                + "<button type='button' class='btn btn-default' data-dismiss='modal' data-target='#updateBndModal' data-toggle='modal'>"
                + "<span class='glyphicon glyphicon-edit'></span>" + "</button>" + "</td>" + "</tr>";
    }
    Criteria criteriaCount = session.createCriteria(Brand.class);
    criteriaCount.setProjection(Projections.rowCount());
    totalNumberOfRecords = (int) (long) (Long) criteriaCount.uniqueResult();
    int noOfPages = totalNumberOfRecords / numberOfRecordsPerPage;

    if (totalNumberOfRecords > (noOfPages * numberOfRecordsPerPage)) {
        noOfPages = noOfPages + 1;
    }

    for (int j = 1; j <= noOfPages; j++) {
        if (noOfPages > 1) {
            String myurl = "manufactures.jsp?pgIndex=" + j;
            if (j == pageIndex) {
                s2 += "<span class='current'>" + j + "</span>";
            } else {
                s2 += "<a href='" + myurl + "'>" + j + "</a>";
            }

        }
    }
    session.close();
    response.setContentType("application/json");
    response.setCharacterEncoding("UTF-8");
    response.getWriter().write("[{\"d1\":\"" + s1 + "\",\"d2\":\"" + s2 + "\"}]");
}

From source file:com.certus.actions.orderFilterAction.java

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    Session session = com.certus.connection.HibernateUtil.getSessionFactory().openSession();
    int pageIndex = 0;
    int totalNumberOfRecords = 0;
    int numberOfRecordsPerPage = 9;

    String sPageIndex = request.getParameter("pgIndex");
    pageIndex = sPageIndex == null ? 1 : Integer.parseInt(sPageIndex);
    int s = (pageIndex * numberOfRecordsPerPage) - numberOfRecordsPerPage;
    if (request.getParameter("startDate") != null) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date sDate = null;//from   w  w  w .java2s  .  co  m
        Date eDate = null;
        try {

            sDate = sdf.parse(request.getParameter("startDate"));
            eDate = sdf.parse(request.getParameter("endDate"));
        } catch (Exception e) {
        }
        List<Order> oList = session.createCriteria(Order.class, "ord")
                .add(Restrictions.between("ord.dateOrdered", sDate, eDate)).setFirstResult(s)
                .setMaxResults(numberOfRecordsPerPage).list();

        String s1 = "", s2 = "";
        for (Order order : oList) {
            s1 += "<tr>" + "<td>" + order.getInvoNum() + "</td>" + "<td>" + order.getName() + "</td>" + "<td>"
                    + order.getStatus() + "</td>" + "<td> Rs. " + order.getGrandTot() + "</td>"
                    + "<td><button type='button' class='btn btn-default'>"
                    + "<span class='glyphicon glyphicon-eye-open'></span></button>" + "</td>" + "<td>"
                    + "<a href='edit_order.jsp?oid=" + order.getId() + "'>"
                    + "<span class='glyphicon glyphicon-edit'></span>" + "</a>" + "</td>" + "</tr>";
        }
        Criteria criteriaCount = session.createCriteria(Order.class, "ord")
                .add(Restrictions.between("ord.dateOrdered", sDate, eDate));
        criteriaCount.setProjection(Projections.rowCount());
        totalNumberOfRecords = (int) (long) (Long) criteriaCount.uniqueResult();
        int noOfPages = totalNumberOfRecords / numberOfRecordsPerPage;
        if (totalNumberOfRecords > (noOfPages * numberOfRecordsPerPage)) {
            noOfPages = noOfPages + 1;
        }

        for (int j = 1; j <= noOfPages; j++) {
            if (noOfPages > 1) {
                String myurl = "orders.jsp?startDate=" + request.getParameter("startDate") + "&endDate="
                        + request.getParameter("endDate") + "&pgIndex=" + j;
                if (j == pageIndex) {
                    s2 += "<span class='current'>" + j + "</span>";
                } else {
                    s2 += "<a href='" + myurl + "'>" + j + "</a>";
                }

            }
        }

        session.close();
        response.setContentType("application/json");
        response.setCharacterEncoding("UTF-8");
        response.getWriter().write("[{\"d1\":\"" + s1 + "\",\"d2\":\"" + s2 + "\"}]");
    } else {
        List<Order> oList = session.createCriteria(Order.class).setFirstResult(s)
                .setMaxResults(numberOfRecordsPerPage).list();
        String s1 = "", s2 = "";
        for (Order order : oList) {
            s1 += "<tr>" + "<td>" + order.getInvoNum() + "</td>" + "<td>" + order.getName() + "</td>" + "<td>"
                    + order.getStatus() + "</td>" + "<td> Rs. " + order.getGrandTot() + "</td>"
                    + "<td><button type='button' class='btn btn-default'>"
                    + "<span class='glyphicon glyphicon-eye-open'></span></button>" + "</td>" + "<td>"
                    + "<a href='edit_order.jsp?oid=" + order.getId() + "'>"
                    + "<span class='glyphicon glyphicon-edit'></span>" + "</a>" + "</td>" + "</tr>";
        }
        Criteria criteriaCount = session.createCriteria(Order.class);
        criteriaCount.setProjection(Projections.rowCount());
        totalNumberOfRecords = (int) (long) (Long) criteriaCount.uniqueResult();
        int noOfPages = totalNumberOfRecords / numberOfRecordsPerPage;

        if (totalNumberOfRecords > (noOfPages * numberOfRecordsPerPage)) {
            noOfPages = noOfPages + 1;
        }

        for (int j = 1; j <= noOfPages; j++) {
            if (noOfPages > 1) {
                String myurl = "orders.jsp?pgIndex=" + j;
                if (j == pageIndex) {
                    s2 += "<span class='current'>" + j + "</span>";
                } else {
                    s2 += "<a href='" + myurl + "'>" + j + "</a>";
                }

            }
        }
        session.close();
        response.setContentType("application/json");
        response.setCharacterEncoding("UTF-8");
        response.getWriter().write("[{\"d1\":\"" + s1 + "\",\"d2\":\"" + s2 + "\"}]");
    }
}

From source file:com.certus.actions.salesFilterAction.java

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    Session session = com.certus.connection.HibernateUtil.getSessionFactory().openSession();
    int pageIndex = 0;
    int totalNumberOfRecords = 0;
    int numberOfRecordsPerPage = 9;

    String sPageIndex = request.getParameter("pgIndex");
    pageIndex = sPageIndex == null ? 1 : Integer.parseInt(sPageIndex);
    int s = (pageIndex * numberOfRecordsPerPage) - numberOfRecordsPerPage;
    if (request.getParameter("startDate") != null) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date sDate = null;// w  w  w .  java  2s.c om
        Date eDate = null;
        try {

            sDate = sdf.parse(request.getParameter("startDate"));
            eDate = sdf.parse(request.getParameter("endDate"));
        } catch (Exception e) {
        }
        List<Sales> sList = session.createCriteria(Sales.class, "sale")
                .add(Restrictions.between("sale.dateDone", sDate, eDate)).setFirstResult(s)
                .setMaxResults(numberOfRecordsPerPage).list();
        String s1 = "", s2 = "";
        for (Sales sale : sList) {
            s1 += "<tr>" + "<td>" + sale.getId() + "</td>" + "<td>" + sale.getOrder().getUser().getFName() + " "
                    + sale.getOrder().getUser().getLName() + "</td>" + "<td>" + sale.getStatus() + "</td>"
                    + "<td> Rs. " + sale.getGrandTotal() + "</td>" + "<td>" + sale.getDateDone() + "</td>"
                    + "<td>"
                    + "<button type='button' class='btn btn-default'><span class='glyphicon glyphicon-eye-open'></sapn></button>"
                    + "</td>" + "</tr>";
        }
        Criteria criteriaCount = session.createCriteria(Sales.class, "sale")
                .add(Restrictions.between("sale.dateDone", sDate, eDate));
        criteriaCount.setProjection(Projections.rowCount());
        totalNumberOfRecords = (int) (long) (Long) criteriaCount.uniqueResult();
        int noOfPages = totalNumberOfRecords / numberOfRecordsPerPage;
        if (totalNumberOfRecords > (noOfPages * numberOfRecordsPerPage)) {
            noOfPages = noOfPages + 1;
        }

        for (int j = 1; j <= noOfPages; j++) {
            if (noOfPages > 1) {
                String myurl = "sales.jsp?startDate=" + request.getParameter("startDate") + "&endDate="
                        + request.getParameter("endDate") + "&pgIndex=" + j;
                if (j == pageIndex) {
                    s2 += "<span class='current'>" + j + "</span>";
                } else {
                    s2 += "<a href='" + myurl + "'>" + j + "</a>";
                }

            }
        }
        session.close();

        response.setContentType("application/json");
        response.setCharacterEncoding("UTF-8");
        response.getWriter().write("[{\"d1\":\"" + s1 + "\",\"d2\":\"" + s2 + "\"}]");
    } else {
        List<Sales> sList = session.createCriteria(Sales.class).setFirstResult(s)
                .setMaxResults(numberOfRecordsPerPage).list();
        String s1 = "", s2 = "";
        for (Sales sale : sList) {
            s1 += "<tr>" + "<td>" + sale.getId() + "</td>" + "<td>" + sale.getOrder().getUser().getFName() + " "
                    + sale.getOrder().getUser().getLName() + "</td>" + "<td>" + sale.getStatus() + "</td>"
                    + "<td> Rs. " + sale.getGrandTotal() + "</td>" + "<td>" + sale.getDateDone() + "</td>"
                    + "<td>"
                    + "<button type='button' class='btn btn-default'><span class='glyphicon glyphicon-eye-open'></sapn></button>"
                    + "</td>" + "</tr>";
        }
        Criteria criteriaCount = session.createCriteria(Sales.class);
        criteriaCount.setProjection(Projections.rowCount());
        totalNumberOfRecords = (int) (long) (Long) criteriaCount.uniqueResult();
        int noOfPages = totalNumberOfRecords / numberOfRecordsPerPage;

        if (totalNumberOfRecords > (noOfPages * numberOfRecordsPerPage)) {
            noOfPages = noOfPages + 1;
        }

        for (int j = 1; j <= noOfPages; j++) {
            if (noOfPages > 1) {
                String myurl = "sales.jsp?pgIndex=" + j;
                if (j == pageIndex) {
                    s2 += "<span class='current'>" + j + "</span>";
                } else {
                    s2 += "<a href='" + myurl + "'>" + j + "</a>";
                }

            }
        }
        session.close();
        response.setContentType("application/json");
        response.setCharacterEncoding("UTF-8");
        response.getWriter().write("[{\"d1\":\"" + s1 + "\",\"d2\":\"" + s2 + "\"}]");
    }
}

From source file:com.cimmyt.model.dao.impl.SampleDetailDAOImpl.java

License:Apache License

public List<String> getPlatesNotInShipmentSet(final Integer idLabStudy, final List<String> excludingPlates) {

    List<String> listPlate = (List<String>) getHibernateTemplate()
            .execute(new HibernateCallback<List<String>>() {

                public List<String> doInHibernate(Session session) throws HibernateException, SQLException {
                    Criteria criteria = session.createCriteria(SampleDetail.class);
                    criteria.add(Restrictions.eq("labstudyid.labstudyid", idLabStudy));
                    if (excludingPlates != null && !excludingPlates.isEmpty())
                        criteria.add(Restrictions.not(Restrictions.in("platename", excludingPlates)));

                    criteria.add(Restrictions.eq("selforsend", "N"));
                    criteria.setProjection(Projections.distinct(Projections.property("platename")));
                    criteria.addOrder(Order.asc("platename"));

                    @SuppressWarnings("unchecked")
                    List<String> results = criteria.list();
                    return results;
                }/*w w  w . j a va  2 s . c  om*/

            });
    return listPlate;
}

From source file:com.cms.utils.BaseFWDAOImpl.java

License:Open Source License

public List<T> findByCriteria(String sessionName, int firstResult, int maxResult, ProjectionList projs,
        Criterion[] criterion, Order[] orders) {
    Criteria crit = getSession().createCriteria(model.getClass());
    if (projs != null) {
        crit.setProjection(projs);
    }/*  w w  w . j  av  a2s .c o m*/
    if (criterion != null && criterion.length > 0) {
        for (Criterion c : criterion) {
            crit.add(c);
        }
    }
    if (orders != null && orders.length > 0) {
        for (Order o : orders) {
            crit.addOrder(o);
        }
    }
    if (firstResult > 0) {
        crit.setFirstResult(firstResult);
    }
    if (maxResult > 0) {
        crit.setMaxResults(maxResult);
    }
    return crit.list();
}

From source file:com.court.controller.AssignNewLoanFxmlController.java

private ObservableList<Loan> getAllLoans() {
    Session session = HibernateUtil.getSessionFactory().openSession();
    Criteria c = session.createCriteria(Loan.class);
    c.add(Restrictions.eq("status", true));
    c.setProjection(Projections.projectionList().add(Projections.property("loanId"), "loanId")
            .add(Projections.property("loanName"), "loanName"));
    c.setResultTransformer(Transformers.aliasToBean(Loan.class));
    List<Loan> lList = c.list();
    ObservableList<Loan> loans = FXCollections.observableArrayList(lList);
    session.close();//from   w  w w. j  a  v a  2 s .c  o  m
    return loans;
}

From source file:com.court.controller.AssignNewLoanFxmlController.java

private Set<Member> getAvailableGuarantors() {
    Session session = HibernateUtil.getSessionFactory().openSession();
    Criteria c1 = session.createCriteria(MemberLoan.class);
    c1.setProjection(Projections.projectionList().add(Projections.property("isComplete"), "isComplete")
            .add(Projections.property("guarantors"), "guarantors")
            .add(Projections.property("member"), "member"));
    c1.setResultTransformer(Transformers.aliasToBean(MemberLoan.class));
    List<MemberLoan> ml = c1.list();
    //GET ALL GUARANTORS OF ONGOING LOANS
    List<String> guarantors = ml.stream().filter(p -> !p.isIsComplete())
            .map(m -> m.getGuarantors() + "-" + m.getMember().getMemberId()).collect(Collectors.toList());

    Set<String> lkm = new HashSet<>(guarantors);
    List<String> guarantor_filtered = lkm.stream().map(r -> r = r.split("-")[0]).collect(Collectors.toList());

    //GET ALREADY GUARANTED MEMBERS OF THE GARNTOR
    List<String> alreadyGurantedMembers = ml.stream().filter(p -> !p.isIsComplete())
            .filter(p -> p.getMember().getMemberId().equalsIgnoreCase(getMember().getMemberId()))
            .map(MemberLoan::getGuarantors).collect(Collectors.toList());

    //========================
    Set<Member> set = new HashSet<>();
    //========================

    //GET ALL MEMBERS EXCEPT THE LOAN GRANTOR
    Criteria c2 = session.createCriteria(Member.class);
    c2.add(Restrictions.ne("memberId", getMember().getMemberId()));
    c2.add(Restrictions.eq("status", true));
    c2.setProjection(Projections.projectionList().add(Projections.property("memberId"), "memberId")
            .add(Projections.property("fullName"), "fullName"));
    c2.setResultTransformer(Transformers.aliasToBean(Member.class));

    //IF NO GURANTORS FOUND THEN ALL MEMBERS CAN GUARANT FOR THE LOAN EXCEPT THE LOAN GRANTOR
    if (getUniqueGuarantors(guarantor_filtered, UNIQUE_GUR_FRQUENCY).isEmpty()) {
        List<Member> list = c2.list();
        set.addAll(list);// w w w.  j av  a2s . com
        //
    } else {
        List<Member> list = c2.add(Restrictions
                .not(Restrictions.in("memberId", getUniqueGuarantors(guarantor_filtered, UNIQUE_GUR_FRQUENCY))))
                .list();
        set.addAll(list);
    }

    //IF NO GUARANTORS AVAILABLE THEY CAN GURANT THE GRANTOR ULTIMATELY UNTIL ALL GUARANTED LOANS END
    if (!alreadyGurantedMembers.isEmpty()) {
        Set<Member> arlm = getAlreadyGurantedMembers(alreadyGurantedMembers, session);
        set.addAll(arlm);
    }

    session.close();
    return set;
}

From source file:com.court.controller.AssignNewLoanFxmlController.java

private Set<Member> getAlreadyGurantedMembers(List<String> agm, Session s) {
    Set<String> ug = new HashSet<>();
    for (String string : agm) {
        Type type = new TypeToken<List<String>>() {
        }.getType();/*www  .  ja  v a2  s. com*/
        List<String> yourList = new Gson().fromJson(string, type);
        for (String yl : yourList) {
            ug.add(yl);
        }
    }
    Criteria cc2 = s.createCriteria(Member.class);
    cc2.add(Restrictions.in("memberId", ug));
    cc2.setProjection(Projections.projectionList().add(Projections.property("memberId"), "memberId")
            .add(Projections.property("fullName"), "fullName"));
    cc2.setResultTransformer(Transformers.aliasToBean(Member.class));
    List<Member> list = cc2.list();
    Set<Member> mbrs = new HashSet(list);
    return mbrs;
}

From source file:com.court.controller.CollectionSheetFxmlController.java

private void figureNLoadSearchTextSuggestions(int selectedIndex) {
    Session session = HibernateUtil.getSessionFactory().openSession();
    Criteria c = session.createCriteria(Branch.class);
    c.add(Restrictions.eq("status", true));
    switch (selectedIndex) {
    case 0://from  w  ww  .ja  v a 2s.co m
        // c.setProjection(Projections.property("branchName"));
        c.add(Restrictions.eq("parentId", 0));
        List<Branch> bNames = c.list();
        //=== ADDED NEW RESTRICTION TO GET ONLY PAYMENT OFFICES INTO AUTO-COMPLETE==========
        autoCompletionList(bNames);
        break;
    case 1:
        c.setProjection(Projections.property("memberId"));
        List<String> mIds = c.list();
        // autoCompletionList(mIds);
        break;
    case 2:
        c.setProjection(Projections.property("nameWithIns"));
        List<String> mNames = c.list();
        // autoCompletionList(mNames);
        break;
    }
    session.close();
}