i have trouble with JPQL statement, i created NameQuery followed:
我有JPQL语句的问题,我创建了NameQuery跟随:
@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:
在无状态bean中,我创建了一个方法来激活此查询:
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:
之后在JSF Managed Bean中我创建了一个方法测试,如果用户ID和项目ID存在于DB中,它会将用户ID和itemid打印到服务器:
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?
但是当客户端将项目A添加到数据库中然后将项目B添加到数据库中,并再次将项目A添加到数据库中时,它不会打印项目ID的项目ID,它会打印项目B的项目ID。为什么?如果我创建select语句findByItemid,它可以很好地工作,如果我按照要求它的意思是当点击添加A然后单击添加B,并再次添加A它打印ID 1-2-1,但如果我使用选择语句查找按ItemId和UserId它不起作用,它会打印itemid是1-2-2它发生了什么?
1 个解决方案
#1
1
you can delete 2 line
你可以删除2行
cartPk.setUserid(userid);
cartPk.setItemid(itemid);
i think when you assign cart = cartDAO.getUserItemCart(userid, itemid);
我想当你分配cart = cartDAO.getUserItemCart(userid,itemid);
the userid and itemid are have value.
userid和itemid有值。
#1
1
you can delete 2 line
你可以删除2行
cartPk.setUserid(userid);
cartPk.setItemid(itemid);
i think when you assign cart = cartDAO.getUserItemCart(userid, itemid);
我想当你分配cart = cartDAO.getUserItemCart(userid,itemid);
the userid and itemid are have value.
userid和itemid有值。