开发者

How to check exist item in cart and update it to 1 if user add it again?

i have trouble with JPQL statement, i created NameQuery followed:

 @NamedQuery(name = "Cart.findExistCart", query = "SELECT c FROM Cart c WHERE  c.cartPK.userid = :userid AND c.cartPK.itemid = :itemid "),

in stateless bean i create one method to active this query followed:

 public Cart getUserItemCart(int userid,int itemid){

         Query query = em.createNamedQuery("Cart.findExistCart");
        // query.setParameter("userid", userid);
         query.setParameter("itemid", itemid);
         query.setParameter("userid", userid);

   开发者_运维百科      return (Cart) query.getSingleResult();

     }

after that in JSF Managed Bean i create one method test if user id and item id are exist in DB it will print user id and itemid to server:

public void addtoCart(Items item){
        this.items = item;
        int userid = getMemberLoginController().getUser().getUserid().intValue();
        int itemid = getItems().getItemid().intValue();
        if (cartDAO.getUserItemCart(userid, itemid) != null ){


              // cart = cartDAO.getUserItemCart(userid, itemid);
               cartPk.setUserid(userid);
               cartPk.setItemid(itemid);
               cart.setCartPK(cartPk);
               cart = cartDAO.getUserItemCart(userid, itemid);

               System.out.println(cart.getCartQuantity());


    }

but when client add item A into db and then add item B into db, and once again add item A into DB , it don't print item id of item id, it print item id of item B. Why? if i create select statement findByItemid, it work well if i do upon requirement it mean when click add A then click add B , and add A again it print id 1-2-1, but if i use select statement find Cart by ItemId and UserId it not work, it will print itemid is 1-2-2 What's happen with it?


you can delete 2 line

       cartPk.setUserid(userid);
       cartPk.setItemid(itemid);

i think when you assign cart = cartDAO.getUserItemCart(userid, itemid);

the userid and itemid are have value.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜