public boolean addToCart(Cart cart) {
    Session session = HibernateUtil.openSession();

    Transaction tx = null;
    try {
      tx = session.getTransaction();
      tx.begin();
      session.saveOrUpdate(cart);
      tx.commit();
    } catch (Exception e) {
      if (tx != null) {
        tx.rollback();
      }
      e.printStackTrace();
    } finally {
      session.close();
    }
    return true;
  }
 public List<Cart> getListOfProductsInCart() {
   List<Cart> list = new ArrayList<Cart>();
   Session session = HibernateUtil.openSession();
   Transaction tx = null;
   try {
     tx = session.getTransaction();
     tx.begin();
     list = session.createQuery("from Cart").list();
     tx.commit();
   } catch (Exception e) {
     if (tx != null) {
       tx.rollback();
     }
     e.printStackTrace();
   } finally {
     session.close();
   }
   return list;
 }
 public List<Cart> getCartByUserId(String uid) {
   List<Cart> list = new ArrayList<Cart>();
   Session session = HibernateUtil.openSession();
   Transaction tx = null;
   try {
     tx = session.getTransaction();
     tx.begin();
     Query query = session.createQuery("from Cart where user_id=" + uid);
     list = query.list();
     tx.commit();
   } catch (Exception e) {
     if (tx != null) {
       tx.rollback();
     }
     e.printStackTrace();
   } finally {
     session.close();
   }
   return list;
 }
 public Cart getCartById(String pid) {
   Cart cart = new Cart();
   Session session = HibernateUtil.openSession();
   Transaction tx = null;
   try {
     tx = session.getTransaction();
     tx.begin();
     Query query = session.createQuery("from Cart where cart_id=" + pid);
     cart = (Cart) query.uniqueResult();
     tx.commit();
   } catch (Exception e) {
     if (tx != null) {
       tx.rollback();
     }
     e.printStackTrace();
   } finally {
     session.close();
   }
   return cart;
 }