Example usage for org.hibernate Criteria setFirstResult

List of usage examples for org.hibernate Criteria setFirstResult

Introduction

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

Prototype

public Criteria setFirstResult(int firstResult);

Source Link

Document

Set the first result to be retrieved.

Usage

From source file:com.apt.facade.AdminFacade.java

public List<Admin> getAdminList(int page, int pagesize) {
    List<Admin> lst = new ArrayList<>();

    Session session = null;/*w  w w.  j a  v a2  s  .co  m*/
    Transaction trans = null;

    try {
        session = HibernateUtil.getSessionFactory().getCurrentSession();
        trans = session.beginTransaction();

        Criteria crit = session.createCriteria(Admin.class);
        crit.setMaxResults(pagesize);
        crit.setFirstResult((page - 1) * pagesize);
        lst = crit.list();
        trans.commit();

    } catch (Exception e) {
        e.printStackTrace();
        if (trans != null) {
            trans.rollback();
        }
    } finally {
        if (session != null && session.isConnected()) {
            session.close();
        }
    }

    return lst;
}

From source file:com.apt.facade.AdminFacade.java

public List<Admin> getAdminList(AdminFinder finder, int page, int recordPerPage) {
    List<Admin> lst = new ArrayList<>();

    Session session = null;//from   w w w  . j  a v a  2 s  .co  m
    Transaction trans = null;

    try {
        session = HibernateUtil.getSessionFactory().getCurrentSession();
        trans = session.beginTransaction();

        Criteria crit = session.createCriteria(Admin.class);
        crit.add(Restrictions.sqlRestriction("1=1"));
        if (finder.getAdminID() != null) {
            crit.add(Restrictions.and(Restrictions.eq("adminID", finder.getAdminID())));
        }
        if (finder.getAdminName() != null) {
            crit.add(Restrictions.and(Restrictions.ilike("adminName", "%" + finder.getAdminName() + "%")));
        }
        if (finder.getStatus() != null) {
            crit.add(Restrictions.and(Restrictions.eq("status", finder.getStatus())));
        }
        crit.setFirstResult((page - 1) * recordPerPage);
        crit.setMaxResults(recordPerPage);
        lst = crit.list();
        trans.commit();

    } catch (Exception e) {
        e.printStackTrace();
        if (trans != null) {
            trans.rollback();
        }
    } finally {
        if (session != null && session.isConnected()) {
            session.close();
        }
    }

    return lst;
}

From source file:com.apt.facade.AssignmentFacade.java

public List<Assignment> getAssignmentList(AssignmentFinder finder, int page, int recordPerPage) {
    List<Assignment> lst = new ArrayList<>();

    Session session = null;/*from   w w  w. j a  v a 2 s . co m*/
    Transaction trans = null;

    try {
        session = HibernateUtil.getSessionFactory().getCurrentSession();
        trans = session.beginTransaction();

        Criteria crit = session.createCriteria(Assignment.class);

        crit.add(Restrictions.sqlRestriction("1=1"));
        if (finder.getAssignmentId() != null) {
            crit.add(Restrictions.and(Restrictions.eq("assignmentId", finder.getAssignmentId())));
        }
        if (finder.getAssignmentName() != null) {
            crit.add(Restrictions
                    .and(Restrictions.ilike("assignmentName", "%" + finder.getAssignmentName() + "%")));
        }
        if (finder.getBatch() != null) {
            crit.add(Restrictions.and(Restrictions.eq("batch", finder.getBatch())));
        }
        if (finder.getSubject() != null) {
            crit.add(Restrictions.and(Restrictions.eq("subject", finder.getSubject())));
        }
        if (finder.getStatus() != null) {
            crit.add(Restrictions.and(Restrictions.eq("status", finder.getStatus())));
        }
        if (finder.getStarttime() != null) {
            crit.add(Restrictions.and(Restrictions.ge("startTime", finder.getStarttime())));
        }
        if (finder.getEndtime() != null) {
            crit.add(Restrictions.and(Restrictions.le("endTime", finder.getEndtime())));
        }
        crit.setFirstResult((page - 1) * recordPerPage);
        crit.setMaxResults(recordPerPage);
        lst = crit.list();
        trans.commit();

    } catch (Exception e) {
        e.printStackTrace();
        if (trans != null) {
            trans.rollback();
        }
    } finally {
        if (session != null && session.isConnected()) {
            session.close();
        }
    }

    return lst;
}

From source file:com.apt.facade.BatchFacade.java

public List<Batch> getBatchList(BatchFinder finder, int page, int recordPerPage) {
    List<Batch> lst = new ArrayList<>();

    Session session = null;/*from   w w  w.j  av  a2 s.  c  o m*/
    Transaction trans = null;

    try {
        session = HibernateUtil.getSessionFactory().getCurrentSession();
        trans = session.beginTransaction();

        Criteria crit = session.createCriteria(Batch.class);
        crit.add(Restrictions.sqlRestriction("1=1"));
        if (finder.getBatchId() != null) {
            crit.add(Restrictions.and(Restrictions.eq("batchId", finder.getBatchId())));
        }
        if (finder.getBatchName() != null) {
            crit.add(Restrictions.and(Restrictions.ilike("batchName", "%" + finder.getBatchName() + "%")));
        }
        crit.setFirstResult((page - 1) * recordPerPage);
        crit.setMaxResults(recordPerPage);
        lst = crit.list();
        trans.commit();

    } catch (Exception e) {
        e.printStackTrace();
        if (trans != null) {
            trans.rollback();
        }
    } finally {
        if (session != null && session.isConnected()) {
            session.close();
        }
    }

    return lst;
}

From source file:com.apt.facade.StudentFacade.java

public List<Student> getStudentList(StudentFinder finder, int page, int recordPerPage) {
    List<Student> lst = new ArrayList<>();

    Session session = null;/*  w  ww  .j av a 2 s  .c om*/
    Transaction trans = null;

    try {
        session = HibernateUtil.getSessionFactory().getCurrentSession();
        trans = session.beginTransaction();

        Criteria crit = session.createCriteria(Student.class);

        crit.add(Restrictions.sqlRestriction("1=1"));
        if (finder.getStudentId() != null) {
            crit.add(Restrictions.and(Restrictions.eq("studentId", finder.getStudentId())));
        }
        if (finder.getStudentName() != null) {
            crit.add(Restrictions.and(Restrictions.ilike("studentName", "%" + finder.getStudentName() + "%")));
        }
        if (finder.getBatch() != null) {
            crit.createAlias("batch", "a");
            Criterion rest = Restrictions.or(Restrictions.eq("a.batchId", finder.getBatch().getBatchId()),
                    Restrictions.eq("a.batchName", finder.getBatch().getBatchName()));
            crit.add(Restrictions.and(rest));
        }
        crit.setFirstResult((page - 1) * recordPerPage);
        crit.setMaxResults(recordPerPage);
        lst = crit.list();
        trans.commit();

    } catch (Exception e) {
        e.printStackTrace();
        if (trans != null) {
            trans.rollback();
        }
    } finally {
        if (session != null && session.isConnected()) {
            session.close();
        }
    }

    return lst;
}

From source file:com.apt.facade.SubjectFacade.java

public List<Subject> getSubjectList(SubjectFinder finder, int page, int recordPerPage) {
    List<Subject> lst = new ArrayList<>();

    Session session = null;//  w  ww. ja  v a  2 s. c  o m
    Transaction trans = null;

    try {
        session = HibernateUtil.getSessionFactory().getCurrentSession();
        trans = session.beginTransaction();

        Criteria crit = session.createCriteria(Subject.class);
        crit.add(Restrictions.sqlRestriction("1=1"));
        if (finder.getSubjectId() != null) {
            crit.add(Restrictions.and(Restrictions.eq("subjectId", finder.getSubjectId())));
        }
        if (finder.getSubjectName() != null) {
            crit.add(Restrictions.and(Restrictions.ilike("subjectName", "%" + finder.getSubjectName() + "%")));
        }
        crit.setFirstResult((page - 1) * recordPerPage);
        crit.setMaxResults(recordPerPage);
        lst = crit.list();
        trans.commit();

    } catch (Exception e) {
        e.printStackTrace();
        if (trans != null) {
            trans.rollback();
        }
    } finally {
        if (session != null && session.isConnected()) {
            session.close();
        }
    }

    return lst;
}

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

@Override
public Map findByQuery(CustomerQuery query) {
    int pageSize = Configs.PAGE_SIZE;
    Criteria criteria = factory.getCurrentSession().createCriteria(Customer.class);
    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 w  w  .  j a v a 2  s.c om*/
            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("company", query.getCompany().replace("*", "%").replace("?", "_")));
        } else {
            criteria.add(Expression.eq("company", 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()));
    }
    List<CustomerType> types = new ArrayList<CustomerType>();
    if (query.getFlagCompany().equals("Y")) {
        types.add(CustomerType.C);
    }
    if (query.getFlagPersonal().equals("Y")) {
        types.add(CustomerType.P);
    }

    if (types.size() > 0) {
        criteria.add(Expression.in("customerType", types.toArray()));
    }

    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<Customer> 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.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  www.  ja va  2 s  .  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.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 . j av  a  2  s.  c  o  m*/
            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 {/* w  w w.  j a v 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()));
        }
    }
    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;
}