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.arg.arsoft.siantluis.repository.imp.OrderRepository.java

@Override
public Map findByQuery(OrderQuery query) {
    int pageSize = Configs.PAGE_SIZE;
    Criteria criteria = factory.getCurrentSession().createCriteria(Order.class);

    if (query.getCode() != null && !query.getCode().equals("")) {
        if (query.getCode().contains("*") || query.getCode().contains("?")) {
            criteria.add(Restrictions.like("code", query.getCode().replace("*", "%").replace("?", "_")));
        } else {//from  ww  w .jav  a 2s.  co  m
            criteria.add(Restrictions.eq("code", query.getCode()));
        }
    }
    if (query.getName() != null && !query.getName().equals("")) {
        if (query.getName().contains("*") || query.getName().contains("?")) {
            criteria.add(Restrictions.like("name", query.getName().replace("*", "%").replace("?", "_")));
        } else {
            criteria.add(Restrictions.eq("name", query.getName()));
        }
    }
    if (query.getEmployee() != null && !query.getEmployee().equals("")) {
        criteria.createAlias("requestBy", "e", JoinType.LEFT_OUTER_JOIN);
        criteria.add(Restrictions.eq("e.code", query.getCode()));
    }
    if (query.getCustomer() != null && !query.getCustomer().equals("")) {
        criteria.createAlias("customer", "c", JoinType.LEFT_OUTER_JOIN);
        criteria.add(Restrictions.eq("c.code", query.getCustomer()));
    }
    if (query.getShipper() != null && query.getShipper() != 0) {
        criteria.createAlias("shipper", "s", JoinType.LEFT_OUTER_JOIN);
        criteria.add(Restrictions.eq("s.id", query.getShipper()));
    }
    if (query.getOrderDateStart() != null) {
        criteria.add(Restrictions.ge("orderDate", query.getOrderDateStart()));
    }
    if (query.getOrderDateEnd() != null) {
        criteria.add(Restrictions.le("orderDate", query.getOrderDateEnd()));
    }
    if (query.getRequireDateStart() != null) {
        criteria.add(Restrictions.ge("requireDate", query.getRequireDateStart()));
    }
    if (query.getRequireDateEnd() != null) {
        criteria.add(Restrictions.le("requireDate", query.getRequireDateEnd()));
    }
    long totalRecord = (long) criteria.setProjection(Projections.count(Projections.id().toString()))
            .uniqueResult();
    int start = ((query.getPage() - 1) * pageSize);

    criteria.setProjection(null);
    criteria.setFetchMode("requestBy", FetchMode.JOIN);
    criteria.setFetchMode("approveBy", FetchMode.JOIN);
    criteria.setFetchMode("customer", FetchMode.JOIN);
    criteria.setFetchMode("shipper", FetchMode.JOIN);
    criteria.setFetchMode("address.amphur", FetchMode.JOIN);
    criteria.setFetchMode("address.province", FetchMode.JOIN);

    List<Order> result = criteria.setFirstResult(start).setMaxResults(pageSize)
            .setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY).list();
    long totalPage = totalRecord / pageSize;
    if ((totalRecord % pageSize) > 0) {
        totalPage++;
    }
    List<Integer> pages = new ArrayList<Integer>();
    for (int index = 1; index <= totalPage; index++) {
        pages.add(index);
    }
    Map data = new HashMap();
    data.put("list", result);
    data.put("totalPage", totalPage);
    data.put("totalRecord", totalRecord);
    data.put("pages", pages);

    return data;
}

From source file:com.arg.arsoft.siantluis.repository.imp.PaymentRepository.java

@Override
public Map findByQuery(PaymentQuery query) {
    int pageSize = Configs.PAGE_SIZE;
    Criteria criteria = factory.getCurrentSession().createCriteria(Payment.class);
    if (query.getCode() != null && !query.getCode().equals("")) {
        if (query.getCode().contains("*") || query.getCode().contains("?")) {
            criteria.add(Restrictions.like("code", query.getCode().replace("*", "%").replace("?", "_")));
        } else {/* w  w w .  ja  va2  s  .  c  om*/
            criteria.add(Restrictions.eq("code", query.getCode()));
        }
    }
    if (query.getName() != null && !query.getName().equals("")) {
        if (query.getName().contains("*") || query.getName().contains("?")) {
            criteria.add(Restrictions.like("name", query.getName().replace("*", "%").replace("?", "_")));
        } else {
            criteria.add(Restrictions.eq("name", query.getName()));
        }
    }
    if (query.getPaymentBy() != null && !query.getPaymentBy().equals("")) {
        criteria.createAlias("paymentBy", "p", JoinType.LEFT_OUTER_JOIN);
        criteria.add(Restrictions.eq("p.code", query.getCode()));
    }
    if (query.getPaymentDateFrom() != null) {
        criteria.add(Restrictions.ge("paymentDate", query.getPaymentDateFrom()));
    }
    if (query.getPaymentDateTo() != null) {
        criteria.add(Restrictions.le("paymentDate", query.getPaymentDateTo()));
    }
    if (query.getDepartment() != null && query.getDepartment() != 0) {
        criteria.createAlias("department", "d", JoinType.LEFT_OUTER_JOIN);
        criteria.add(Restrictions.eq("d.id", query.getDepartment()));
    }
    if (query.getBranch() != null && query.getBranch() != 0) {
        criteria.createAlias("branch", "b", JoinType.LEFT_OUTER_JOIN);
        criteria.add(Restrictions.eq("b.id", query.getBranch()));
    }
    long totalRecord = (long) criteria.setProjection(Projections.count(Projections.id().toString()))
            .uniqueResult();

    criteria.setProjection(null);

    int start = ((query.getPage() - 1) * pageSize);

    // fetch assco
    criteria.setFetchMode("paymentBy", FetchMode.JOIN);
    criteria.setFetchMode("department", FetchMode.JOIN);
    criteria.setFetchMode("branch", FetchMode.JOIN);

    List<Payment> result = criteria.setResultTransformer(Criteria.ROOT_ENTITY).setFirstResult(start)
            .setMaxResults(pageSize).list();

    long totalPage = totalRecord / pageSize;
    if ((totalRecord % pageSize) > 0) {
        totalPage++;
    }
    List<Integer> pages = new ArrayList<Integer>();
    for (int index = 1; index <= totalPage; index++) {
        pages.add(index);
    }

    Map data = new HashMap();
    data.put("list", result);
    data.put("totalPage", totalPage);
    data.put("totalRecord", totalRecord);
    data.put("pages", pages);

    System.out.println(pages.size());
    return data;
}

From source file:com.arg.arsoft.siantluis.repository.imp.ProductRepository.java

@Override
public Map findByQuery(ProductQuery query) {
    int pageSize = Configs.PAGE_SIZE;
    Criteria criteria = factory.getCurrentSession().createCriteria(Product.class);
    if (query.getCode() != null && !query.getCode().equals("")) {
        if (query.getCode().contains("*") || query.getCode().contains("?")) {
            criteria.add(org.hibernate.criterion.Restrictions.like("code",
                    query.getCode().replace("*", "%").replace("?", "_")));

        } else {//w  w  w. ja va  2  s . com
            criteria.add(org.hibernate.criterion.Restrictions.eq("code", query.getCode()));
        }
    }
    if (query.getName() != null && !query.getName().equals("")) {
        if (query.getName().contains("*") || query.getName().contains("?")) {
            criteria.add(org.hibernate.criterion.Restrictions.like("name",
                    query.getName().replace("*", "%").replace("?", "_")));
        } else {
            criteria.add(org.hibernate.criterion.Restrictions.eq("name", query.getName()));
        }
    }
    if (query.getSupplier() != null && !query.getSupplier().equals("")) {
        criteria.createAlias("supplier", "s", JoinType.LEFT_OUTER_JOIN);
        criteria.add(org.hibernate.criterion.Restrictions.eq("s.code", query.getSupplier()));
    }
    if (query.getActive() != null && !query.getActive().equals("")) {
        ProductActive active = ProductActive.valueOf(query.getActive());
        criteria.add(org.hibernate.criterion.Restrictions.eq("active", active));
    }
    long totalRecord = (long) criteria.setProjection(Projections.count(Projections.id().toString()))
            .uniqueResult();
    int start = ((query.getPage() - 1) * pageSize);

    criteria.setProjection(null);
    criteria.setFetchMode("supplier", FetchMode.EAGER);
    List<Product> result = criteria.setFirstResult(start).setMaxResults(pageSize)
            .setResultTransformer(Criteria.ROOT_ENTITY).list();

    long totalPage = totalRecord / pageSize;
    if ((totalRecord % pageSize) > 0) {
        totalPage++;
    }
    List<Integer> pages = new ArrayList<Integer>();
    for (int index = 1; index <= totalPage; index++) {
        pages.add(index);
    }
    Map data = new HashMap();
    data.put("list", result);
    data.put("totalPage", totalPage);
    data.put("totalRecord", totalRecord);
    data.put("pages", pages);

    return data;
}

From source file:com.arg.arsoft.siantluis.repository.imp.ProjectRepository.java

@Override
public Map findByQuery(ProjectQuery query) {
    int pageSize = Configs.PAGE_SIZE;
    Criteria criteria = factory.getCurrentSession().createCriteria(Project.class);
    if (query.getCode() != null && !query.getCode().equals("")) {
        if (query.getCode().contains("*") || query.getCode().contains("?")) {
            criteria.add(Restrictions.like("code", query.getCode().replace("*", "%").replace("?", "_")));
        } else {/*from www.  j a v  a  2  s. c  o  m*/
            criteria.add(Restrictions.eq("code", query.getCode()));
        }
    }
    if (query.getName() != null && !query.getName().equals("")) {
        if (query.getName().contains("*") || query.getName().contains("?")) {
            criteria.add(Restrictions.like("name", query.getName().replace("*", "%").replace("?", "_")));
        } else {
            criteria.add(Restrictions.eq("name", query.getName()));
        }
    }
    long totalRecord = (long) criteria.setProjection(Projections.count(Projections.id().toString()))
            .uniqueResult();
    criteria.setProjection(null);
    int start = ((query.getPage() - 1) * pageSize);
    List<Project> result = criteria.setFirstResult(start).setMaxResults(pageSize)
            .setResultTransformer(Criteria.ROOT_ENTITY).list();

    long totalPage = totalRecord / pageSize;
    if ((totalRecord % pageSize) > 0) {
        totalPage++;
    }
    List<Integer> pages = new ArrayList<Integer>();
    for (int index = 1; index <= totalPage; index++) {
        pages.add(index);
    }

    Map data = new HashMap();
    data.put("list", result);
    data.put("totalPage", totalPage);
    data.put("totalRecord", totalRecord);
    data.put("pages", pages);

    System.out.println(pages.size());
    return data;
}

From source file:com.arg.arsoft.siantluis.repository.imp.PurchaseRepository.java

@Override
public Map findByQuery(PurchaseQuery query) {
    int pageSize = Configs.PAGE_SIZE;
    Criteria criteria = factory.getCurrentSession().createCriteria(Purchase.class);
    if (query.getCode() != null && !query.getCode().equals("")) {
        if (query.getCode().contains("*") || query.getCode().contains("?")) {
            criteria.add(Restrictions.like("code", query.getCode().replace("*", "%").replace("?", "_")));
        } else {/*from w w w  . j a va2  s . c  o  m*/
            criteria.add(Restrictions.eq("code", query.getCode()));
        }
    }
    if (query.getName() != null && !query.getName().equals("")) {
        if (query.getName().contains("*") || query.getName().contains("?")) {
            criteria.add(Restrictions.like("name", query.getName().replace("*", "%").replace("?", "_")));
        } else {
            criteria.add(Restrictions.eq("name", query.getName()));
        }
    }
    if (query.getPurchaseDateStart() != null) {
        criteria.add(Restrictions.ge("purchaseDate", query.getPurchaseDateStart()));
    }
    if (query.getPurchaseDateEnd() != null) {
        criteria.add(Restrictions.le("purchaseDate", query.getPurchaseDateEnd()));
    }
    if (query.getPurchaseBy() != null && !query.getPurchaseBy().equals("")) {
        criteria.createAlias("purchaseBy", "e", JoinType.LEFT_OUTER_JOIN);
        criteria.add(Restrictions.eq("e.code", query.getPurchaseBy()));
    }
    if (query.getSupplier() != null && !query.getSupplier().equals("")) {
        criteria.createAlias("supplier", "s", JoinType.LEFT_OUTER_JOIN);
        criteria.add(Restrictions.eq("s.code", query.getSupplier()));
    }
    if (query.getDepartment() != null && query.getDepartment() != 0) {
        criteria.createAlias("department", "d", JoinType.LEFT_OUTER_JOIN);
        criteria.add(Restrictions.eq("d.id", query.getDepartment()));
    }
    if (query.getBranch() != null && query.getBranch() != 0) {
        criteria.createAlias("branch", "b", JoinType.LEFT_OUTER_JOIN);
        criteria.add(Restrictions.eq("b.id", query.getBranch()));
    }
    if (query.getStatus() != null && !query.getStatus().equals("")) {
        PurchaseStatus status = PurchaseStatus.valueOf(query.getStatus());
        criteria.add(Restrictions.eq("status", status));
    }

    long totalRecord = (long) criteria.setProjection(Projections.count(Projections.id().toString()))
            .uniqueResult();
    criteria.setProjection(null);
    int start = ((query.getPage() - 1) * pageSize);
    criteria.setFetchMode("department", FetchMode.JOIN);
    criteria.setFetchMode("branch", FetchMode.JOIN);
    criteria.setFetchMode("supplier", FetchMode.JOIN);
    criteria.setFetchMode("purchaseBy", FetchMode.JOIN);
    List<Purchase> result = criteria.setFirstResult(start).setMaxResults(pageSize)
            .setResultTransformer(Criteria.ROOT_ENTITY).list();

    long totalPage = totalRecord / pageSize;
    if ((totalRecord % pageSize) > 0) {
        totalPage++;
    }
    List<Integer> pages = new ArrayList<Integer>();
    for (int index = 1; index <= totalPage; index++) {
        pages.add(index);
    }

    Map data = new HashMap();
    data.put("list", result);
    data.put("totalPage", totalPage);
    data.put("totalRecord", totalRecord);
    data.put("pages", pages);

    System.out.println(pages.size());
    return data;
}

From source file:com.arg.arsoft.siantluis.repository.imp.ReservationRepository.java

@Override
public Map findByQuery(ReservationQuery query) {
    int pageSize = Configs.PAGE_SIZE;
    Criteria criteria = factory.getCurrentSession().createCriteria(Reservation.class);
    if (query.getCode() != null && !query.getCode().equals("")) {
        if (query.getCode().contains("*") || query.getCode().contains("?")) {
            criteria.add(Restrictions.like("code", query.getCode().replace("*", "%").replace("?", "_")));
        } else {//from  w ww  . ja  va 2s .  c  om
            criteria.add(Restrictions.eq("code", query.getCode()));
        }
    }
    if (query.getDescription() != null && !query.getDescription().equals("")) {
        if (query.getDescription().contains("*") || query.getDescription().contains("?")) {
            criteria.add(Restrictions.like("description",
                    query.getDescription().replace("*", "%").replace("?", "*")));
        } else {
            criteria.add(Restrictions.eq("description", query.getDescription()));
        }
    }
    if (query.getRequestBy() != null && !query.getRequestBy().equals("")) {
        criteria.createAlias("reservationBy", "em", JoinType.LEFT_OUTER_JOIN);
        criteria.add(Restrictions.eq("em.code", query.getRequestBy()));
    }
    if (query.getReservationDateFrom() != null) {
        criteria.add(Restrictions.ge("reservationDate", query.getReservationDateFrom()));
    }
    if (query.getReservationDateTo() != null) {
        criteria.add(Restrictions.le("reservationDate", query.getReservationDateTo()));
    }

    long totalRecord = (long) criteria.setProjection(Projections.count(Projections.id().toString()))
            .uniqueResult();
    int start = ((query.getPage() - 1) * pageSize);

    criteria.setProjection(null);

    criteria.setFetchMode("reservationBy", FetchMode.JOIN);

    List<Reservation> result = criteria.setFirstResult(start).setMaxResults(pageSize)
            .setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY).list();
    long totalPage = totalRecord / pageSize;
    if ((totalRecord % pageSize) > 0) {
        totalPage++;
    }
    List<Integer> pages = new ArrayList<Integer>();
    for (int index = 1; index <= totalPage; index++) {
        pages.add(index);
    }
    Map data = new HashMap();
    data.put("list", result);
    data.put("totalPage", totalPage);
    data.put("totalRecord", totalRecord);
    data.put("pages", pages);

    return data;
}

From source file:com.arg.arsoft.siantluis.repository.imp.RoleRepository.java

@Override
public Map findByQuery(RoleQuery query) {
    int pageSize = Configs.PAGE_SIZE;

    Criteria criteria = factory.getCurrentSession().createCriteria(Role.class);

    if (query.getCode() != null && !query.getCode().equals("")) {
        if (query.getCode().contains("*") || query.getCode().contains("?")) {
            criteria.add(Restrictions.like("code", query.getCode().replace("*", "%").replace("?", "_")));
        } else {//from  ww  w  .  j a  v  a  2  s .  c  o m
            criteria.add(Restrictions.eq("code", query.getCode()));
        }
    }
    if (query.getName() != null && !query.getName().equals("")) {
        if (query.getName().contains("*") || query.getName().contains("?")) {
            criteria.add(Restrictions.like("name", query.getName().replace("*", "%").replace("?", "_")));
        } else {
            criteria.add(Restrictions.eq("name", query.getName()));
        }
    }
    long totalRecord = (long) criteria.setProjection(Projections.count(Projections.id().toString()))
            .uniqueResult();
    int start = ((query.getPage() - 1) * pageSize);
    criteria.setProjection(null);

    List<Role> result = criteria.setFirstResult(start).setMaxResults(pageSize)
            .setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY).list();
    long totalPage = totalRecord / pageSize;
    if ((totalRecord % pageSize) > 0) {
        totalPage++;
    }
    List<Integer> pages = new ArrayList<Integer>();
    for (int index = 1; index <= totalPage; index++) {
        pages.add(index);
    }
    Map data = new HashMap();
    data.put("list", result);
    data.put("totalPage", totalPage);
    data.put("totalRecord", totalRecord);
    data.put("pages", pages);

    return data;
}

From source file:com.arg.arsoft.siantluis.repository.imp.SupplierRepository.java

@Override
public Map findByQuery(SupplierQuery query) {
    int pageSize = Configs.PAGE_SIZE;
    Criteria criteria = factory.getCurrentSession().createCriteria(Supplier.class);

    //  criteria.createAlias("address.amphur","a",JoinType.LEFT_OUTER_JOIN);

    if (query.getCode() != null && !query.getCode().equals("")) {
        if (query.getCode().contains("*") || query.getCode().contains("?")) {
            criteria.add(Expression.like("code", query.getCode().replace("*", "%").replace("?", "_")));
        } else {//from w  ww  .j  a va2s . co m
            criteria.add(Expression.eq("code", query.getCode()));
        }
    }
    if (query.getName() != null && !query.getName().equals("")) {
        if (query.getName().contains("*") || query.getName().contains("?")) {
            criteria.add(Expression.like("name", query.getName().replace("*", "%").replace("?", "_")));
        } else {
            criteria.add(Expression.eq("name", query.getName()));
        }
    }
    if (query.getCompany() != null && !query.getCompany().equals("")) {
        if (query.getCompany().contains("*") || query.getCompany().contains("?")) {
            criteria.add(Expression.like("compny", query.getCompany().replace("*", "%").replace("?", "_")));
        } else {
            criteria.add(Expression.eq("compny", query.getCompany()));
        }
    }
    if (query.getContactName() != null && !query.getContactName().equals("")) {
        if (query.getContactName().contains("*") || query.getContactName().contains("?")) {
            criteria.add(Expression.like("contact.contactName",
                    query.getContactName().replace("*", "%").replace("?", "_")));

        } else {
            criteria.add(Expression.eq("contact.contactName", query.getContactName()));
        }
    }
    if (query.getProvince() != null && query.getProvince() != 0) {
        criteria.createAlias("address.province", "p", JoinType.LEFT_OUTER_JOIN);
        criteria.add(Expression.eq("p.id", query.getProvince()));
    }

    long totalRecord = (long) criteria.setProjection(Projections.count(Projections.id().toString()))
            .uniqueResult();

    criteria.setProjection(null);

    int start = ((query.getPage() - 1) * pageSize);
    criteria.setFetchMode("address.province", FetchMode.JOIN);
    criteria.setFetchMode("address.amphur", FetchMode.JOIN);
    List<Supplier> result = criteria.setFirstResult(start).setMaxResults(pageSize)
            .setResultTransformer(Criteria.ROOT_ENTITY).list();

    long totalPage = totalRecord / pageSize;
    if ((totalRecord % pageSize) > 0) {
        totalPage++;
    }
    List<Integer> pages = new ArrayList<Integer>();
    for (int index = 1; index <= totalPage; index++) {
        pages.add(index);
    }

    Map data = new HashMap();
    data.put("list", result);
    data.put("totalPage", totalPage);
    data.put("totalRecord", totalRecord);
    data.put("pages", pages);

    System.out.println(pages.size());
    return data;
}

From source file:com.arg.arsoft.siantluis.repository.imp.UserRepository.java

@Override
public Map findByQuery(UserQuery query) {
    int pageSize = Configs.PAGE_SIZE;

    Criteria criteria = factory.getCurrentSession().createCriteria(User.class);

    if (query.getCode() != null && !query.getCode().equals("")) {
        if (query.getCode().contains("*") || query.getCode().contains("?")) {
            criteria.add(Restrictions.like("code", query.getCode().replace("*", "%").replace("?", "_")));
        } else {//from   ww w. j av  a 2  s.  c  o  m
            criteria.add(Restrictions.eq("code", query.getCode()));
        }
    }
    if (query.getName() != null && !query.getName().equals("")) {
        if (query.getName().contains("*") || query.getName().contains("?")) {
            criteria.add(Restrictions.like("name", query.getName().replace("*", "%").replace("?", "_")));
        } else {
            criteria.add(Restrictions.eq("name", query.getName()));
        }
    }
    long totalRecord = (long) criteria.setProjection(Projections.count(Projections.id().toString()))
            .uniqueResult();
    int start = ((query.getPage() - 1) * pageSize);
    criteria.setProjection(null);

    List<User> result = criteria.setFirstResult(start).setMaxResults(pageSize)
            .setResultTransformer(Criteria.ROOT_ENTITY).list();
    long totalPage = totalRecord / pageSize;
    if ((totalRecord % pageSize) > 0) {
        totalPage++;
    }
    List<Integer> pages = new ArrayList<Integer>();
    for (int index = 1; index <= totalPage; index++) {
        pages.add(index);
    }
    Map data = new HashMap();
    data.put("list", result);
    data.put("totalPage", totalPage);
    data.put("totalRecord", totalRecord);
    data.put("pages", pages);

    return data;
}

From source file:com.bahadirakin.dao.impl.BaseHibernateDAO.java

License:Apache License

protected Object findByProjection(Projection projection, Criterion... criterions) {
    Object result = null;/*w w w  .j  a va 2  s .  c  o  m*/
    try {
        Session session = this.getCurrentSession();
        Transaction transaction = session.beginTransaction();
        Criteria criteria = createCriteria(session, criterions);
        criteria.setProjection(projection);
        result = criteria.uniqueResult();
        transaction.commit();
    } catch (Exception e) {
        LOG.error("Error while findByCriteria Entities. M: " + e.getMessage() + " C: " + e.getCause(), e);
    }
    return result;
}