Example usage for org.hibernate Criteria setFetchMode

List of usage examples for org.hibernate Criteria setFetchMode

Introduction

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

Prototype

public Criteria setFetchMode(String associationPath, FetchMode mode) throws HibernateException;

Source Link

Document

Specify an association fetching strategy for an association or a collection of values.

Usage

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

@Override
public Order findByKey(Integer key) {
    Criteria criteria = factory.getCurrentSession().createCriteria(Order.class);
    criteria.add(Restrictions.eq("id", key));

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

    criteria.setFetchMode("items", FetchMode.JOIN);
    criteria.setFetchMode("items.product", FetchMode.JOIN);

    List<Order> result = criteria.list();
    if (result != null && result.size() > 0) {
        return result.get(0);
    } else {//  w w  w.j  a  v  a  2  s. c o m
        return null;
    }
}

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 {//  w  w  w  .  j a va  2s . 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.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 Payment findByKey(Integer key) {
    Criteria criteria = factory.getCurrentSession().createCriteria(Payment.class);
    criteria.add(Restrictions.eq("id", key));

    // fetch assocication
    criteria.setFetchMode("paymentBy", FetchMode.JOIN);
    criteria.setFetchMode("approveBy", FetchMode.JOIN);
    criteria.setFetchMode("department", FetchMode.JOIN);
    criteria.setFetchMode("branch", FetchMode.JOIN);
    criteria.setFetchMode("attachment", FetchMode.JOIN);

    // fetch collection
    criteria.setFetchMode("purchases", FetchMode.JOIN);
    criteria.setFetchMode("expenses", FetchMode.JOIN);
    criteria.setFetchMode("purchases.purchase", FetchMode.JOIN);
    criteria.setFetchMode("expenses.expense", FetchMode.JOIN);
    criteria.setFetchMode("cashes", FetchMode.JOIN);
    criteria.setFetchMode("cheques", FetchMode.JOIN);
    criteria.setFetchMode("cheques.bank", FetchMode.JOIN);
    criteria.setFetchMode("credits", FetchMode.JOIN);
    criteria.setFetchMode("credits.bank", FetchMode.JOIN);
    criteria.setFetchMode("transfers", FetchMode.JOIN);
    criteria.setFetchMode("transfers.bank", FetchMode.JOIN);

    List<Payment> result = criteria.list();
    if (result != null && result.size() > 0) {
        return result.get(0);
    } else {/*  ww w .  j  a v a  2 s . c  o m*/
        return null;
    }
}

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 {//from w  w w. jav  a  2 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 Product findByKey(Integer key) {
    Criteria criteria = factory.getCurrentSession().createCriteria(Product.class);
    criteria.add(Restrictions.eq("id", key));
    criteria.setFetchMode("supplier", FetchMode.JOIN);
    List<Product> result = criteria.setResultTransformer(Criteria.ROOT_ENTITY).list();
    if (result != null && result.size() > 0) {
        return result.get(0);
    } else {/*from ww w.j  a  v a2s .c om*/
        return null;
    }
}

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 {//from  ww  w .j  a v  a2 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 Project findByKey(Integer key) {
    Criteria criteria = factory.getCurrentSession().createCriteria(Project.class);
    criteria.add(Restrictions.eq("id", key));
    criteria.setFetchMode("items", FetchMode.JOIN);
    criteria.setFetchMode("items.frms", FetchMode.JOIN);
    criteria.setFetchMode("items.frms.productGroup", FetchMode.JOIN);
    List<Project> result = criteria.list();
    if (result != null && result.size() > 0) {
        return result.get(0);
    } else {// ww  w . j  a va2 s. c  om
        return null;
    }
}

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

@Override
public Purchase findByKey(Integer key) {
    Criteria criteria = factory.getCurrentSession().createCriteria(Purchase.class);
    criteria.add(Restrictions.eq("id", key));
    criteria.setFetchMode("approveBy", FetchMode.JOIN);
    criteria.setFetchMode("purchaseBy", FetchMode.JOIN);
    criteria.setFetchMode("department", FetchMode.JOIN);
    criteria.setFetchMode("supplier", FetchMode.JOIN);
    criteria.setFetchMode("branch", FetchMode.JOIN);

    criteria.setFetchMode("products", FetchMode.JOIN);
    criteria.setFetchMode("services", FetchMode.JOIN);
    criteria.setFetchMode("products.product", FetchMode.JOIN);
    criteria.setFetchMode("services.service", FetchMode.JOIN);

    List<Purchase> result = criteria.list();
    if (result != null && result.size() > 0) {
        return result.get(0);
    } else {//from   ww  w  .  j  a  v a  2  s  .  c  o m
        return null;
    }
}

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 {/* w  w  w  .ja  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 Reservation findByKey(Integer key) {
    Criteria criteria = factory.getCurrentSession().createCriteria(Reservation.class);
    criteria.add(Restrictions.eq("id", key));

    // Fetch Relation
    criteria.setFetchMode("reservationBy", FetchMode.JOIN);
    criteria.setFetchMode("approveBy", FetchMode.JOIN);
    criteria.setFetchMode("attachment", FetchMode.JOIN);

    // Fetch Collection
    criteria.setFetchMode("rooms", FetchMode.JOIN);
    criteria.setFetchMode("its", FetchMode.JOIN);
    criteria.setFetchMode("motors", FetchMode.JOIN);
    criteria.setFetchMode("rooms.equipment", FetchMode.JOIN);
    criteria.setFetchMode("its.equipment", FetchMode.JOIN);
    criteria.setFetchMode("motors.equipment", FetchMode.JOIN);

    List<Reservation> result = criteria.list();
    if (result != null && result.size() > 0) {
        return result.get(0);
    } else {// w w  w.  j a va  2 s .  c  o  m
        return null;
    }

}