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.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  ww  w. j  a v  a2  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.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 {/*w w  w  . j a v a2  s.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 w ww.  j  a v  a2s .  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 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("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 {/*w w  w . ja v  a2  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()));
        }
    }
    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.axway.academy.addressbook.core.AccountManagerImpl.java

License:Open Source License

@SuppressWarnings("unchecked")
@Override/*from  ww w .j av  a  2  s.c  om*/
public List<AddressBookEntry> getAddressBookContacts(AddressBookEntryCriterion criterion, Integer first,
        Integer count) throws NoSuchAccountException {
    Session session = mSessionFactoryManager.getSessionFactory().openSession();

    try {
        if (criterion == null) {
            throw new NoSuchAccountException("AddressBookEntryCriterion not valid");
        }

        Criteria criteria = ((AddressBookEntryCriterionImpl) criterion).getCriteria(session);

        if (first != null) {
            criteria.setFirstResult(first);
        }

        if (count != null) {
            criteria.setMaxResults(count);
        }

        return criteria.list();

    } finally {
        SessionManagerFactory.closeSession(session);
    }
}

From source file:com.Bean.PostinfoHelper.java

public List<Post> searchByCriteria(PostSearcher postSearcher, int page) {

    List<Post> postList = new ArrayList<Post>();
    try {/*from  w w w  .  ja  va2s  . c om*/
        Criteria c = session.createCriteria(Post.class);
        if (postSearcher.getTitle() != null) {
            c.add(Restrictions.like("title", "%" + postSearcher.getTitle() + "%"));
        }
        if ((postSearcher.getMinPrice() >= 0) && (postSearcher.getMaxPrice() >= 0)) {
            c.add(Restrictions.between("price", postSearcher.getMinPrice(), postSearcher.getMaxPrice()));
        } else if (postSearcher.getMinPrice() >= 0) {
            c.add(Restrictions.ge("price", postSearcher.getMinPrice()));
        } else if (postSearcher.getMaxPrice() >= 0) {
            c.add(Restrictions.le("price", postSearcher.getMaxPrice()));
        }
        if (postSearcher.getTypes() != null) {
            c.add(Restrictions.in("type", postSearcher.getTypes()));
        }
        maxPage = c.list().size() / itemsPerPage;
        c.setFirstResult(page * itemsPerPage);
        c.setMaxResults(itemsPerPage);
        postList = (List<Post>) c.list();
        return postList;
    } catch (HibernateException e) {
        System.err.println(e.getMessage());
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

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);/*from  ww  w . jav  a 2 s.c o  m*/
    c.addOrder(Order.desc("Count"));
    c.setFirstResult(0);
    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);/*from ww w  .j  a v  a 2 s.c  o m*/
    c.addOrder(Order.desc("Count"));
    c.setFirstResult(0);
    c = selectTopPostMostFav == 0 ? c.setMaxResults(10) : c.setMaxResults(selectTopPostMostFav);
    List<Object[]> a = c.list();
    return a;
}

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

@Override
public PaginationData get(int first, int items) {
    Criteria criteria = getSession().createCriteria(registeredClass());
    criteria.setFirstResult(first);
    criteria.setMaxResults(items);//from  w w w . j a v  a  2 s.  com
    List<T> entities = criteria.list();
    HibernateInitSupport.setCls(registeredClass());
    for (T e : entities)
        HibernateInitSupport.initDomain((T) e);

    PaginationData paginationData = new PaginationData(count(), items, first, entities);
    return paginationData;
}