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.bean.StatisticBean.java

public LineChartModel getStatisticViewInYear() {
    LineChartModel combinedModel = new LineChartModel();

    BarChartSeries boys = new BarChartSeries();
    boys.setLabel("Post");

    Date now = new Date();
    int year = yearStatisticViewInYear == 0 ? now.getYear() : yearStatisticViewInYear;
    Dao dao = new Dao();
    //1/* w ww.  jav a2 s. com*/
    Date from1 = new Date(year, 0, 1);
    Date to1 = new Date(year, 0, 31);
    Criterion logic1 = Restrictions.between("proCreateDate", from1, to1);
    int count1 = dao.getByCondition(Property.class, logic1).size();
    Criteria c = dao.createCriteria(Property.class);
    c.add(Restrictions.between("proCreateDate", from1, to1));
    c.setProjection(Projections.sum("proHit"));
    Object o = c.uniqueResult();
    long view1 = (long) (o == null ? 0L : o);
    //2
    Date from2 = new Date(year, 1, 1);
    Date to2 = new Date(year, 1, 28);
    Criterion logic2 = Restrictions.between("proCreateDate", from2, to2);
    int count2 = dao.getByCondition(Property.class, logic2).size();
    Criteria c2 = dao.createCriteria(Property.class);
    c2.add(Restrictions.between("proCreateDate", from2, to2));
    c2.setProjection(Projections.sum("proHit"));
    Object o2 = c2.uniqueResult();
    long view2 = (long) ((o2 == null) ? 0L : o2);
    //3
    Date from3 = new Date(year, 2, 1);
    Date to3 = new Date(year, 2, 31);
    Criterion logic3 = Restrictions.between("proCreateDate", from3, to3);
    int count3 = dao.getByCondition(Property.class, logic3).size();
    Criteria c3 = dao.createCriteria(Property.class);
    c3.add(Restrictions.between("proCreateDate", from3, to3));
    c3.setProjection(Projections.sum("proHit"));
    Object o3 = c3.uniqueResult();
    long view3 = (long) ((o3 == null) ? 0L : o3);
    //4
    Date from4 = new Date(year, 3, 1);
    Date to4 = new Date(year, 3, 30);
    Criterion logic4 = Restrictions.between("proCreateDate", from4, to4);
    int count4 = dao.getByCondition(Property.class, logic4).size();
    Criteria c4 = dao.createCriteria(Property.class);
    c4.add(Restrictions.between("proCreateDate", from4, to4));
    c4.setProjection(Projections.sum("proHit"));
    Object o4 = c4.uniqueResult();
    long view4 = (long) ((o4 == null) ? 0L : o4);
    //5
    Date from5 = new Date(year, 4, 1);
    Date to5 = new Date(year, 4, 31);
    Criterion logic5 = Restrictions.between("proCreateDate", from5, to5);
    int count5 = dao.getByCondition(Property.class, logic5).size();
    Criteria c5 = dao.createCriteria(Property.class);
    c5.add(Restrictions.between("proCreateDate", from5, to5));
    c5.setProjection(Projections.sum("proHit"));
    Object o5 = c5.uniqueResult();
    long view5 = (long) ((o5 == null) ? 0L : o5);
    //6
    Date from6 = new Date(year, 5, 1);
    Date to6 = new Date(year, 5, 30);
    Criterion logic6 = Restrictions.between("proCreateDate", from6, to6);
    int count6 = dao.getByCondition(Property.class, logic6).size();
    Criteria c6 = dao.createCriteria(Property.class);
    c6.add(Restrictions.between("proCreateDate", from6, to6));
    c6.setProjection(Projections.sum("proHit"));
    Object o6 = c6.uniqueResult();
    long view6 = (long) ((o6 == null) ? 0L : o6);
    //7
    Date from7 = new Date(year, 6, 1);
    Date to7 = new Date(year, 6, 31);
    Criterion logic7 = Restrictions.between("proCreateDate", from7, to7);
    int count7 = dao.getByCondition(Property.class, logic7).size();
    Criteria c7 = dao.createCriteria(Property.class);
    c7.add(Restrictions.between("proCreateDate", from7, to7));
    c7.setProjection(Projections.sum("proHit"));
    Object o7 = c7.uniqueResult();
    long view7 = (long) ((o7 == null) ? 0L : o7);
    //8
    Date from8 = new Date(year, 7, 1);
    Date to8 = new Date(year, 7, 31);
    Criterion logic8 = Restrictions.between("proCreateDate", from8, to8);
    int count8 = dao.getByCondition(Property.class, logic8).size();
    Criteria c8 = dao.createCriteria(Property.class);
    c8.add(Restrictions.between("proCreateDate", from8, to8));
    c8.setProjection(Projections.sum("proHit"));
    Object o8 = c8.uniqueResult();
    long view8 = (long) ((o8 == null) ? 0L : o8);
    //9
    Date from9 = new Date(year, 8, 1);
    Date to9 = new Date(year, 8, 30);
    Criterion logic9 = Restrictions.between("proCreateDate", from9, to9);
    int count9 = dao.getByCondition(Property.class, logic9).size();
    Criteria c9 = dao.createCriteria(Property.class);
    c9.add(Restrictions.between("proCreateDate", from9, to9));
    c9.setProjection(Projections.sum("proHit"));
    Object o9 = c9.uniqueResult();
    long view9 = (long) ((o9 == null) ? 0L : o9);
    //10
    Date from10 = new Date(year, 9, 1);
    Date to10 = new Date(year, 9, 31);
    Criterion logic10 = Restrictions.between("proCreateDate", from10, to10);
    int count10 = dao.getByCondition(Property.class, logic10).size();
    Criteria c10 = dao.createCriteria(Property.class);
    c10.add(Restrictions.between("proCreateDate", from10, to10));
    c10.setProjection(Projections.sum("proHit"));
    Object o10 = c10.uniqueResult();
    long view10 = (long) ((o10 == null) ? 0L : o10);
    //11
    Date from11 = new Date(year, 10, 1);
    Date to11 = new Date(year, 10, 30);
    Criterion logic11 = Restrictions.between("proCreateDate", from11, to11);
    int count11 = dao.getByCondition(Property.class, logic11).size();
    Criteria c11 = dao.createCriteria(Property.class);
    c11.add(Restrictions.between("proCreateDate", from11, to11));
    c11.setProjection(Projections.sum("proHit"));
    Object o11 = c11.uniqueResult();
    long view11 = (long) ((o11 == null) ? 0L : o11);
    //12
    Date from12 = new Date(year, 11, 1);
    Date to12 = new Date(year, 11, 31);
    Criterion logic12 = Restrictions.between("proCreateDate", from12, to12);
    int count12 = dao.getByCondition(Property.class, logic12).size();
    Criteria c12 = dao.createCriteria(Property.class);
    c12.add(Restrictions.between("proCreateDate", from12, to12));
    c12.setProjection(Projections.sum("proHit"));
    Object o12 = c12.uniqueResult();
    long view12 = (long) ((o12 == null) ? 0L : o12);

    boys.set("01", count1);
    boys.set("02", count2);
    boys.set("03", count3);
    boys.set("04", count4);
    boys.set("05", count5);
    boys.set("06", count6);
    boys.set("07", count7);
    boys.set("08", count8);
    boys.set("09", count9);
    boys.set("10", count10);
    boys.set("11", count11);
    boys.set("12", count12);

    LineChartSeries girls = new LineChartSeries();
    girls.setLabel("View");
    girls.setYaxis(AxisType.Y2);
    girls.set("01", view1);
    girls.set("02", view2);
    girls.set("03", view3);
    girls.set("04", view4);
    girls.set("05", view5);
    girls.set("06", view6);
    girls.set("07", view7);
    girls.set("08", view8);
    girls.set("09", view9);
    girls.set("10", view10);
    girls.set("11", view11);
    girls.set("12", view12);

    combinedModel.addSeries(boys);
    combinedModel.addSeries(girls);

    combinedModel.setTitle("Statistic view of post in year");
    combinedModel.setLegendPosition("ne");
    combinedModel.setMouseoverHighlight(true);
    combinedModel.setShowDatatip(true);
    combinedModel.setShowPointLabels(true);

    combinedModel.getAxes().put(AxisType.X, new CategoryAxis("Month"));
    combinedModel.getAxes().put(AxisType.X2, new CategoryAxis("Period"));

    Axis yAxis = combinedModel.getAxis(AxisType.Y);
    yAxis.setMin(0);
    yAxis.setMax(200);

    Axis y2Axis = new LinearAxis("Number");
    y2Axis.setMin(0);
    y2Axis.setMax(100000);

    combinedModel.getAxes().put(AxisType.Y2, y2Axis);

    return combinedModel;
}

From source file:com.bean.StatisticBean.java

public List<Object[]> getTopUserMostPost() {
    Dao dao = new Dao();
    Criteria c = dao.createCriteria(Property.class);
    ProjectionList proj = Projections.projectionList();
    proj.add(Projections.groupProperty("users"));
    proj.add(Projections.rowCount(), "Count");
    c.setProjection(proj);
    c.addOrder(Order.desc("Count"));
    c.setFirstResult(0);//from w  w w .  ja va  2s. co m
    c = selectTopUserMostPost == 0 ? c.setMaxResults(10) : c.setMaxResults(selectTopUserMostPost);
    List<Object[]> a = c.list();
    return a;
}

From source file:com.bean.StatisticBean.java

public List<Object[]> getTopPostMostFav() {
    Dao dao = new Dao();
    Criteria c = dao.createCriteria(FavoriteProperty.class);
    ProjectionList proj = Projections.projectionList();
    proj.add(Projections.groupProperty("property"));
    proj.add(Projections.rowCount(), "Count");
    c.setProjection(proj);
    c.addOrder(Order.desc("Count"));
    c.setFirstResult(0);//  w  ww  .  j a v a2 s.  c o m
    c = selectTopPostMostFav == 0 ? c.setMaxResults(10) : c.setMaxResults(selectTopPostMostFav);
    List<Object[]> a = c.list();
    return a;
}

From source file:com.bluexml.side.Framework.alfresco.jbpm.CustomJBPMEngine.java

License:Open Source License

/**
 * Construct a JBPM Hibernate query based on the Task Query provided
 * /*from w  ww  .j  ava  2  s .  c o  m*/
 * @param session
 * @param query
 * @return  jbpm hiberate query criteria
 */
private Criteria createTaskQueryCriteria(Session session, WorkflowTaskQuery query) {
    Criteria task = session.createCriteria(TaskInstance.class);

    // task id
    if (query.getTaskId() != null) {
        task.add(Restrictions.eq("id", getJbpmId(query.getTaskId())));
    }

    // task state
    if (query.getTaskState() != null) {
        WorkflowTaskState state = query.getTaskState();
        if (state == WorkflowTaskState.IN_PROGRESS) {
            task.add(Restrictions.eq("isOpen", true));
            task.add(Restrictions.isNull("end"));
        } else if (state == WorkflowTaskState.COMPLETED) {
            task.add(Restrictions.eq("isOpen", false));
            task.add(Restrictions.isNotNull("end"));
        }
    }

    // task name
    if (query.getTaskName() != null) {
        task.add(Restrictions.eq("name", query.getTaskName().toPrefixString(namespaceService)));
    }

    // task actor
    if (query.getActorId() != null) {
        task.add(Restrictions.eq("actorId", query.getActorId()));
    }

    // task custom properties
    if (query.getTaskCustomProps() != null) {
        Map<QName, Object> props = query.getTaskCustomProps();
        if (props.size() > 0) {
            Criteria variables = task.createCriteria("variableInstances");
            Disjunction values = Restrictions.disjunction();
            for (Map.Entry<QName, Object> prop : props.entrySet()) {
                Conjunction value = Restrictions.conjunction();
                value.add(Restrictions.eq("name", factory.mapQNameToName(prop.getKey())));
                value.add(Restrictions.eq("value", prop.getValue().toString()));
                values.add(value);
            }
            variables.add(values);
        }
    }

    // process criteria
    Criteria process = createProcessCriteria(task, query);

    // process custom properties
    if (query.getProcessCustomProps() != null) {
        // TODO: Due to Hibernate bug
        // http://opensource.atlassian.com/projects/hibernate/browse/HHH-957
        // it's not possible to perform a sub-select with the criteria api.
        // For now issue a
        //       secondary query and create an IN clause.
        Map<QName, Object> props = query.getProcessCustomProps();
        if (props.size() > 0) {
            // create criteria for process variables
            Criteria variables = session.createCriteria(VariableInstance.class);
            variables.setProjection(Projections.distinct(Property.forName("processInstance")));
            Disjunction values = Restrictions.disjunction();
            for (Map.Entry<QName, Object> prop : props.entrySet()) {
                Conjunction value = Restrictions.conjunction();
                value.add(Restrictions.eq("name", factory.mapQNameToName(prop.getKey())));
                value.add(Restrictions.eq("value", prop.getValue().toString()));
                values.add(value);
            }
            variables.add(values);

            // note: constrain process variables to same criteria as tasks
            createProcessCriteria(variables, query);

            Disjunction processIdCriteria = createProcessIdCriteria(variables);

            // constrain tasks by process list
            process = (process == null) ? task.createCriteria("processInstance") : process;
            process.add(processIdCriteria);
        }
    }

    // order by
    if (query.getOrderBy() != null) {
        WorkflowTaskQuery.OrderBy[] orderBy = query.getOrderBy();
        for (WorkflowTaskQuery.OrderBy orderByPart : orderBy) {
            if (orderByPart == WorkflowTaskQuery.OrderBy.TaskActor_Asc) {
                task.addOrder(Order.asc("actorId"));
            } else if (orderByPart == WorkflowTaskQuery.OrderBy.TaskActor_Desc) {
                task.addOrder(Order.desc("actorId"));
            } else if (orderByPart == WorkflowTaskQuery.OrderBy.TaskCreated_Asc) {
                task.addOrder(Order.asc("create"));
            } else if (orderByPart == WorkflowTaskQuery.OrderBy.TaskCreated_Desc) {
                task.addOrder(Order.desc("create"));
            } else if (orderByPart == WorkflowTaskQuery.OrderBy.TaskDue_Asc) {
                task.addOrder(Order.asc("dueDate"));
            } else if (orderByPart == WorkflowTaskQuery.OrderBy.TaskDue_Desc) {
                task.addOrder(Order.desc("dueDate"));
            } else if (orderByPart == WorkflowTaskQuery.OrderBy.TaskId_Asc) {
                task.addOrder(Order.asc("id"));
            } else if (orderByPart == WorkflowTaskQuery.OrderBy.TaskId_Desc) {
                task.addOrder(Order.desc("id"));
            } else if (orderByPart == WorkflowTaskQuery.OrderBy.TaskName_Asc) {
                task.addOrder(Order.asc("name"));
            } else if (orderByPart == WorkflowTaskQuery.OrderBy.TaskName_Desc) {
                task.addOrder(Order.desc("name"));
            } else if (orderByPart == WorkflowTaskQuery.OrderBy.TaskState_Asc) {
                task.addOrder(Order.asc("end"));
            } else if (orderByPart == WorkflowTaskQuery.OrderBy.TaskState_Desc) {
                task.addOrder(Order.desc("end"));
            }
        }
    }

    // limit results
    if (query.getLimit() != -1) {
        task.setMaxResults(query.getLimit());
    }

    return task;
}

From source file:com.bookselling.dao.RoleDaoImpl.java

@Override
public PaginationData filter(RoleFilterForm criteriaForm, int first, int items) {
    //parse to neccessary form
    RoleFilterForm form = criteriaForm;/*from   ww  w.  j  a v a  2  s. co m*/

    Criteria criteria = getSession().createCriteria(Role.class);

    //get data from filter form
    String keyword = form.getKeyword();
    Set<Permission> permissions = form.getPermissions();
    Integer permissionIds[] = new Integer[permissions.size()];
    for (int i = 0; i < permissions.size(); i++)
        permissionIds[i] = permissions.iterator().next().getId();

    //setup criteria
    criteria.createAlias("permissions", "psm").add(Restrictions.like("name", "%" + keyword + "%"))
            .add(Restrictions.in("psm.id", permissionIds));

    //start querying
    List<Role> roles = criteria.list();

    //init proxy obj
    for (Role role : roles)
        HibernateInitSupport.initRole(role);

    //pagination
    PaginationData paginationData = new PaginationData(
            (long) criteria.setProjection(Projections.rowCount()).uniqueResult(), items, first, roles);

    return paginationData;
}

From source file:com.bookselling.dao.SellerInvoiceDaoImpl.java

@Override
public PaginationData<SellerInvoice> get(User user, int first, int items) {
    Criteria criteria = getSession().createCriteria(SellerInvoice.class);
    criteria.createAlias("seller", "sl").add(Restrictions.eq("sl.id", user.getId()));

    criteria.setFirstResult(first);/*  w ww .  ja v a2s. c  o  m*/
    criteria.setMaxResults(items);

    Set<SellerInvoice> sellerInvoices = new HashSet<>(criteria.list());
    HibernateInitSupport.setCls(SellerInvoice.class);
    for (SellerInvoice invoice : sellerInvoices)
        HibernateInitSupport.initDomain(invoice);

    PaginationData paginationData = new PaginationData(
            (long) criteria.setProjection(Projections.rowCount()).uniqueResult(), items, first, sellerInvoices);

    return paginationData;
}

From source file:com.bookselling.dao.SellerInvoiceDaoImpl.java

private Object[] filterCriteria(SellerInvoiceFilterForm form, int first, int items, int id) {
    String keyword = form.getKeyword();
    SellerInvoiceFilterType searchBy = form.getSearchBy();
    Date fromDate = form.getFromDate();
    Date toDate = form.getToDate();
    Double fromPrice = form.getFromPrice();
    Double toPrice = form.getToPrice();
    SellerInvoiceOrderType orderBy = form.getOrderBy();
    SortType sortType = form.getSortType();

    Criteria criteria = getSession().createCriteria(SellerInvoice.class);
    criteria.createAlias("seller", "sl").createAlias("buyer", "bye").createAlias("bye.account", "acc");

    if (keyword != null) {
        keyword = "%" + keyword + "%";
        if (searchBy == SellerInvoiceFilterType.BUYER)
            criteria.add(Restrictions.like("acc.username", keyword));
        else if (searchBy == SellerInvoiceFilterType.OWNER) {
            Name name = new Name();
            name.setName(keyword);//from  www .  j a v  a 2s.c o  m
            criteria.add(Restrictions.like("bye.name", name));
        } else if (searchBy == SellerInvoiceFilterType.ADDRESS) {
            Address address = new Address();
            address.setAddress(keyword);
            criteria.add(Restrictions.like("contact.address", address));
        } else if (searchBy == SellerInvoiceFilterType.PHONE) {
            PhoneNumber phone = new PhoneNumber();
            phone.setPhoneNumber(keyword);
            criteria.add(Restrictions.like("contact.phoneNumber", phone));
        }
    }

    if (fromDate != null)
        criteria.add(Restrictions.ge("createdDate", fromDate));
    if (toDate != null)
        criteria.add(Restrictions.le("createdDate", toDate));

    if (fromPrice != null)
        criteria.add(Restrictions.ge("totalPrice", fromPrice));
    if (toPrice != null)
        criteria.add(Restrictions.le("totalPrice", toPrice));

    String propertyName = null;
    if (orderBy == SellerInvoiceOrderType.BUYER)
        propertyName = "acc.username";
    else if (orderBy == SellerInvoiceOrderType.OWNER)
        propertyName = "bye.name";
    else if (orderBy == SellerInvoiceOrderType.DATE)
        propertyName = "createdDate";
    else if (orderBy == SellerInvoiceOrderType.PRICE)
        propertyName = "totalPrice";

    if (id != -1)
        criteria.add(Restrictions.eq("sl.id", id));

    //Ly s dng
    long rowCount = (long) criteria.setProjection(Projections.countDistinct("id")).uniqueResult();

    //Ly id
    criteria.setProjection(null).setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)
            .setProjection(Projections.distinct(Projections.id())).setFirstResult(first).setMaxResults(items)
            .addOrder(sortType == SortType.ASC ? Order.asc(propertyName) : Order.desc(propertyName));

    List<Integer> ids = new ArrayList<>();
    for (Iterator<Integer> temp = criteria.list().iterator(); temp.hasNext();)
        ids.add(temp.next());

    //Criteria ph
    Criteria subCriteria = getSession().createCriteria(SellerInvoice.class);
    subCriteria.createAlias("seller", "sl").createAlias("buyer", "bye").createAlias("bye.account", "acc")
            .add(Restrictions.in("id", ids.size() > 0 ? ids : Arrays.asList(-1)))
            .addOrder(sortType == SortType.ASC ? Order.asc(propertyName) : Order.desc(propertyName));

    return new Object[] { subCriteria, rowCount };
}

From source file:com.bookselling.dao.SellingPostDaoImpl.java

private Object[] genericFilter(SellingPostFilterForm criteriaForm, int first, int items, int id) {
    Criteria criteria = getSession().createCriteria(SellingPost.class);

    //parse form//from   w w w .  j av a 2s  .c  om
    SellingPostFilterForm form = criteriaForm;

    //get form data
    String keyword = form.getKeyword();
    Double minPrice = form.getMinPrice();
    Double maxPrice = form.getMaxPrice();
    QuatityFilterType quatityStatus = form.getQuatityStatus();
    SellingPostFilterType searchBy = form.getSearchBy();
    SellingPostStatus sellingPostStatus[] = form.getSellingPostStatus();
    Set<Subject> subjects = form.getSubjects();
    Integer subjectIds[] = new Integer[subjects.size()];
    Subject subjectsArray[] = subjects.toArray(new Subject[subjectIds.length]);
    for (int i = 0; i < subjects.size(); i++)
        subjectIds[i] = subjectsArray[i].getId();

    //create criteria
    criteria.createAlias("purchasingSellingEntity", "bk").createAlias("bk.subjects", "sbj")
            .createAlias("bk.publisher", "pub").createAlias("seller", "sl").createAlias("sl.account", "acc");

    //search by keyword
    if (keyword != null && !keyword.isEmpty()) {
        keyword = "%" + keyword.trim() + "%";
        if (searchBy == SellingPostFilterType.HEADER)
            criteria.add(Restrictions.like("header", keyword));
        else if (searchBy == SellingPostFilterType.NAME) {
            criteria.add(Restrictions.like("bk.name", keyword)); //product
        } else if (searchBy == SellingPostFilterType.PUBLISHER) {
            criteria.add(Restrictions.like("pub.name", keyword));
        }
    }

    //search with price range        
    criteria.add(Restrictions.between("bk.sellingPrice", minPrice == null ? 0 : minPrice,
            maxPrice == null ? Integer.MAX_VALUE : maxPrice));

    //search with quatity status
    if (quatityStatus == QuatityFilterType.AVAILABLE)
        criteria.add(Restrictions.gt("bk.quatity", 0));
    else if (quatityStatus == QuatityFilterType.OUTOFSTOCK)
        criteria.add(Restrictions.eq("bk.quatity", 0));

    //search with selling post status
    if (sellingPostStatus.length != 0)
        criteria.add(Restrictions.in("status", sellingPostStatus));

    //search with subjects
    if (subjectIds.length != 0)
        criteria.add(Restrictions.in("sbj.id", subjectIds));

    //get data from form
    SortType sortType = form.getSortType();
    SellingPostOrderType sortByProperty = form.getSortByProperty();

    //Set up criteria
    String propertyName = null;
    if (sortByProperty == SellingPostOrderType.HEADER)
        propertyName = "header";
    else if (sortByProperty == SellingPostOrderType.NAME)
        propertyName = "bk.name";
    else if (sortByProperty == SellingPostOrderType.PUBLISHER)
        propertyName = "pub.name";
    else if (sortByProperty == SellingPostOrderType.CREATEDDATE)
        propertyName = "createdDate";
    else if (sortByProperty == SellingPostOrderType.PRICE)
        propertyName = "bk.sellingPrice";

    if (id != -1)
        criteria.add(Restrictions.eq("sl.id", id));

    //Ly s dng
    long rowCount = (long) criteria.setProjection(Projections.countDistinct("id")).uniqueResult();

    //Ly id
    criteria.setProjection(null).setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)
            .setProjection(Projections.distinct(Projections.id())).setFirstResult(first).setMaxResults(items)
            .addOrder(sortType == SortType.ASC ? Order.asc(propertyName) : Order.desc(propertyName));

    List<Integer> ids = new ArrayList<>();
    for (Iterator<Integer> temp = criteria.list().iterator(); temp.hasNext();)
        ids.add(temp.next());

    //Criteria ph
    Criteria subCriteria = getSession().createCriteria(SellingPost.class);
    subCriteria.createAlias("purchasingSellingEntity", "bk").createAlias("bk.subjects", "sbj")
            .createAlias("bk.publisher", "pub").createAlias("seller", "sl").createAlias("sl.account", "acc")
            .add(Restrictions.in("id", ids.size() > 0 ? ids : Arrays.asList(-1)))
            .addOrder(sortType == SortType.ASC ? Order.asc(propertyName) : Order.desc(propertyName));

    return new Object[] { subCriteria, rowCount };
}

From source file:com.bookselling.dao.SellingPostDaoImpl.java

@Override
public PaginationData<SellingPost> get(User user, int first, int items) {
    Criteria criteria = getSession().createCriteria(SellingPost.class);
    criteria.createAlias("seller", "sl").add(Restrictions.eq("sl.id", user.getId()));

    criteria.setFirstResult(first);/*from  www .ja  va 2 s .  co  m*/
    criteria.setMaxResults(items);

    List<SellingPost> posts = criteria.list();
    HibernateInitSupport.setCls(SellingPost.class);
    for (SellingPost post : posts)
        HibernateInitSupport.initDomain(post);

    PaginationData paginationData = new PaginationData(
            (long) criteria.setProjection(Projections.countDistinct("id")).uniqueResult(), items, first, posts);

    return paginationData;
}

From source file:com.bookselling.dao.SellingPostDaoImpl.java

@Override
public PaginationData<SellingPost> getConfirmedPost(int first, int items) {
    SQLQuery sqlQuery = getSession().createSQLQuery("");

    Criteria criteria = getSession().createCriteria(SellingPost.class)
            .add(Restrictions.eq("status", SellingPostStatus.CONFIRM));

    //Ly s dng/*from   w ww .j  av a  2 s .co m*/
    long rowCount = (long) criteria.setProjection(Projections.countDistinct("id")).uniqueResult();

    criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)
            .setProjection(Projections.distinct(Projections.id())).addOrder(Order.desc("id"))
            .setFirstResult(first).setMaxResults(items);

    List<Integer> ids = new ArrayList<>();
    for (Iterator<Integer> temp = criteria.list().iterator(); temp.hasNext();)
        ids.add(temp.next());

    //Criteria ph
    Criteria subCriteria = getSession().createCriteria(SellingPost.class);
    subCriteria.createAlias("purchasingSellingEntity", "bk").createAlias("bk.subjects", "sbj")
            .createAlias("bk.publisher", "pub").createAlias("seller", "sl").createAlias("sl.account", "acc")
            .add(Restrictions.in("id", ids.size() > 0 ? ids : Arrays.asList(-1))).addOrder(Order.desc("id"));

    Set<SellingPost> posts = new HashSet(subCriteria.list());
    HibernateInitSupport.setCls(SellingPost.class);
    for (SellingPost post : posts)
        HibernateInitSupport.initDomain(post);

    PaginationData paginationData = new PaginationData(rowCount, items, first, posts);

    return paginationData;
}