コード例 #1
0
  // Get information of product
  public SubProduct getInfoOfProductByProductId(int productId) {
    SubProduct subProduct = new SubProduct();
    Category category = new Category();
    Brand brand = new Brand();
    String hql = "";
    Query query = null;
    // Get product and category and brand
    try {
      hql = "select p.category, p.brand from  Product p  WHERE p.productId = :productId ";
      query = getSession().createQuery(hql);
      query.setParameter("productId", productId);
      List<Object[]> ds = query.list();
      if (ds.size() == 1) {
        Object[] obj = ds.get(0);
        category = (Category) obj[0];
        brand = (Brand) obj[1];
      }
    } catch (Exception e) {
      e.printStackTrace();
      // log.error(e);
    }

    List<Color> listColorOfProduct = colorDao.getListColorOfProduct(productId);
    List<Size> listSizeOfProduct = sizeDao.getListSizeOfProduct(productId);
    DiscountInfo discountInfo = discountDao.getDiscountInfoByProductId(productId);
    // Tax tax = taxDao.getTaxByCategoryId(category.getCategoryId());
    subProduct.setListColor(listColorOfProduct);
    subProduct.setListSize(listSizeOfProduct);
    subProduct.setCategory(category);
    subProduct.setBrand(brand);
    subProduct.setDiscountInfo(discountInfo);
    // subProduct.setTax(tax);
    return subProduct;
  }
コード例 #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;
  }