예제 #1
0
  // Get list of products by category and product name
  // with times, every time 50 products
  public List<Product> getListProductByIdCategoryProductName(
      int categoryId, String productName, int times) {
    Query query = null;
    List<Product> listProduct = new ArrayList<Product>();
    String hql = "";
    int n = (times - 1) * 50;
    int m = times * 50;

    // Get category by categoryId
    Category category = categoryDao.get(categoryId, Category.class);

    try {
      hql =
          "FROM Product p WHERE p.productName LIKE :productName AND p.category = :category ORDER BY p.productName ";
      query = getSession().createQuery(hql);
      query.setString("productName", "%" + productName + "%");
      query.setParameter("category", category);
      if (times > 0) {
        query.setFirstResult(n);
        query.setMaxResults(m);
      }
      listProduct = query.list();
    } catch (Exception e) {
      e.printStackTrace();
      // log.error(e);
    }
    return listProduct;
  }
예제 #2
0
  // Search product by category, brand, color, size, price, name
  // with times, every time 50 products
  public List<Product> searchProductByCategoryBrandColorSizePriceName(
      int categoryId,
      int brandId,
      int colorId,
      int sizeId,
      float fromPrice,
      float toPrice,
      String productName,
      int times) {
    Query query = null;
    List<Product> listProduct = new ArrayList<Product>();
    String hql = "";
    int n = (times - 1) * 50;
    int m = times * 50;

    // Get category by categoryId
    Category category = categoryDao.get(categoryId, Category.class);
    // Get brand by brandId
    Brand brand = brandDao.get(brandId, Brand.class);
    // Get color by colorId
    Color color = colorDao.get(colorId, Color.class);
    // Get size by sizeId
    Size size = sizeDao.get(sizeId, Size.class);

    try {
      // hql = "FROM Product p WHERE p.productName LIKE :productName ";
      hql =
          "SELECT DISTINCT d.product FROM DetailProduct d WHERE d.product.productName LIKE :productName";
      if (category != null) {
        hql = hql + " AND d.product.category = :category ";
      }
      if (brand != null) {
        hql = hql + " AND d.product.brand = :brand ";
      }
      if (color != null) {
        hql = hql + " AND d.color = :color ";
      }
      if (size != null) {
        hql = hql + " AND d.size = :size ";
      }

      hql = hql + " AND d.product.productPrice >= :fromPrice ";
      if (toPrice > 0) {
        hql = hql + " AND d.product.productPrice <= :toPrice ";
      }

      hql += " ORDER BY  d.product.productName ";

      query = getSession().createQuery(hql);
      query.setString("productName", "%" + productName + "%");

      if (category != null) {
        query.setParameter("category", category);
      }
      if (brand != null) {
        query.setParameter("brand", brand);
      }
      if (color != null) {
        query.setParameter("color", color);
      }
      if (size != null) {
        query.setParameter("size", size);
      }

      query.setParameter("fromPrice", fromPrice);
      if (toPrice > 0) {
        query.setParameter("toPrice", toPrice);
      }

      if (times > 0) {
        query.setFirstResult(n);
        query.setMaxResults(m);
      }
      listProduct = query.list();
    } catch (Exception e) {
      e.printStackTrace();
      // log.error(e);
    }
    return listProduct;
  }