// 此方法用于动态条件查询
 public Collection find(Product product) throws ManagerProductException {
   Session session = null;
   Transaction tran = null;
   Collection list = null;
   try {
     session = HibernateUtils.getSession();
     tran = session.beginTransaction();
     Criteria crit = session.createCriteria(Product.class);
     if (product.getAuthor().equals("")) {
       crit.add(Restrictions.like("author", "%"));
     } else {
       crit.add(Restrictions.like("author", "%" + product.getAuthor() + "%"));
     }
     if (product.getName().equals("")) {
       crit.add(Restrictions.like("name", "%"));
     } else {
       crit.add(Restrictions.like("name", "%" + product.getName() + "%"));
     }
     if (product.getPublish().equals("")) {
       crit.add(Restrictions.like("publish", "%"));
     } else {
       crit.add(Restrictions.like("publish", "%" + product.getPublish() + "%"));
     }
     list = crit.list();
     tran.commit();
   } catch (Exception e) {
     e.printStackTrace();
     tran.rollback();
   } finally {
     session.close();
     return list;
   }
 }
Esempio n. 2
0
  public ActionForward orderList(
      ActionMapping mapping,
      ActionForm form,
      HttpServletRequest request,
      HttpServletResponse response)
      throws Exception {
    ActionForward forward = null;

    Session session = null;
    Transaction tran = null;
    IOrderDao dao = new OrderDaoHibernateImp();
    Order order = new Order();
    ShopCart shopCart = (ShopCart) request.getSession().getAttribute("shopCart");

    User user = (User) request.getSession().getAttribute("user");
    int id = Integer.parseInt(request.getParameter("payWay"));

    OrderStatus status = new OrderStatus();
    status.setId(1);
    status.setName("配货状态");
    status.setDescriptions("订单正在配货状态三天内将发出");

    session = HibernateUtils.getSession();
    tran = session.beginTransaction();
    String hql = "from PayWay p where p.id=:id";
    PayWay payWay = (PayWay) session.createQuery(hql).setInteger("id", id).uniqueResult();
    tran.commit();

    if (shopCart == null) {
      System.out.println("shopCart is null.");
      shopCart = new ShopCart();
    }
    order = shopCart.getOrder();
    order.setName(user.getName());
    order.setPayWay(payWay);
    order.setStatus(status);
    order.setUser(user);
    order.setCost(shopCart.getTotal());
    Iterator it = order.getOrderlines().iterator();
    OrderLine orderLine = null;

    while (it.hasNext()) {
      orderLine = (OrderLine) it.next();
      orderLine.setOrder(order);
    }

    dao.addOrder(order);
    List<Order> orderList = (List<Order>) dao.findAll(user.getName());
    request.getSession().setAttribute("orderList", orderList);

    forward = mapping.findForward("orderList");

    return forward;
  }
 public int getTotal() throws ManagerProductException {
   Session session = null;
   Transaction tran = null;
   int total = 0;
   try {
     session = HibernateUtils.getSession();
     tran = session.beginTransaction();
     Query query = session.createQuery(" select  count(*) from  Product ");
     total = ((Integer) query.uniqueResult()).intValue();
     tran.commit();
   } catch (Exception e) {
     tran.rollback();
     e.printStackTrace();
   } finally {
     session.close();
     return total;
   }
 }
 public Collection findAll() throws ManagerProductException {
   Session session = null;
   Transaction tran = null;
   Collection list = null;
   try {
     session = HibernateUtils.getSession();
     tran = session.beginTransaction();
     list = session.createQuery(" from Product ").list();
     tran.commit();
   } catch (Exception e) {
     e.printStackTrace();
     tran.rollback();
     throw new ManagerProductException(e.getMessage());
   } finally {
     session.close();
     return list;
   }
 }
 public Product findById(Integer id) throws ManagerProductException {
   Session session = null;
   Transaction tran = null;
   Product product = null;
   try {
     session = HibernateUtils.getSession();
     tran = session.beginTransaction();
     product = (Product) session.get(Product.class, id);
     tran.commit();
   } catch (Exception e) {
     e.printStackTrace();
     tran.rollback();
     throw new ManagerProductException(e.getMessage());
   } finally {
     session.close();
     return product;
   }
 }
 public Collection find(int start, int max) throws ManagerProductException {
   Session session = null;
   Transaction tran = null;
   Collection list = null;
   try {
     session = HibernateUtils.getSession();
     tran = session.beginTransaction();
     Query query = session.createQuery(" from Product ");
     query.setFirstResult(start);
     query.setMaxResults(max);
     list = query.list();
     tran.commit();
   } catch (Exception e) {
     tran.rollback();
     e.printStackTrace();
   } finally {
     session.close();
     return list;
   }
 }