开发者

EJB3, JPA error with More than one result was returned from Query.getSingleResult()

i have new problem with JPA in EJB3 my stacktrace are:

Caused by: javax.persistence.NonUniqueResultException: More than one result was returned from Query.getSingleResult()
        at org.eclipse.persistence.internal.jpa.EJBQueryImpl.throwNonUniqueResultException(EJBQueryImpl.java:1207)
        at org.eclipse.persistence.internal.jpa.EJBQueryImpl.getSingleResult(EJBQueryImpl.java:722)
        at com.DAO.CartDAO.checkUserID(CartDAO.java:47)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at org.glassfish.ejb.security.application.EJBSecurityManager.runMethod(EJBSecurityManager.java:1056)
        at org.glassfish.ejb.security.application.EJBSecurityManager.invoke(EJBSecurityManager.java:1128)
        at com.sun.ejb.containers.BaseContainer.invokeBeanMethod(BaseContainer.java:5292)
        at com.sun.ejb.EjbInvocation.invokeBeanMethod(EjbInvocation.java:615)
        at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:797)
        at com.sun.ejb.EjbInvocation.proceed(EjbInvocation.java:567)
        at com.sun.ejb.containers.interceptors.SystemInterceptorProxy.doAround(SystemInterceptorProxy.java:157)
        at com.sun.ejb.containers.interceptors.SystemInterceptorProxy.aroundInvoke(SystemInterceptorProxy.java:139)
        at sun.reflect.GeneratedMethodAccessor206.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at com.sun.ejb.containers.interceptors.AroundInvokeInterceptor.intercept(InterceptorManager.java:858)
        at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:797)
        at com.sun.ejb.co

i create one commandbutton to use for add item to cart, and i write one method to add item to cart in DB, i was check if userid and itemid != null, item quantity will add to 1, it mean when i have userid 1 add itemid 1 into cart, my method will check if it existed, item quantity will add 1 to item quantity in cart table, i can do it with one item, but if i add more item two into cart, it mean i add itemid is 2 into cart table, next i add more quantity of itemid 1 into db it throw exception More than one result. . . i know it can't have two instance at t开发者_开发百科he same time, but i don't know how to solve it? Please help me

my addTocart method

public void addtoCart(Items item){
        this.items = item;
        if(cartDAO.checkUserID(getMemberLoginController().getUser().getUserid()) != null &&
                cartDAO.checkItemid(items.getItemid()) != null){
                cart = cartDAO.checkUserID(getMemberLoginController().getUser().getUserid());
                int updateQuantity = cart.getCartQuantity() + 1;
                cart.setCartQuantity(updateQuantity);
                // chuyen so luong sang kieu du lieu big decimal
                java.math.BigDecimal bigQuantity = new java.math.BigDecimal(String.valueOf(updateQuantity));
                // so tien cua san pham = so luong x don gia
                BigDecimal updatePrice = items.getPrice().multiply(bigQuantity);
                cart.setPrice(updatePrice);
                cart = cartDAO.updateCart(cart);

        }else if(cartDAO.checkUserID(getMemberLoginController().getUser().getUserid()) != null &&
                cartDAO.checkItemid(items.getItemid()) == null){
                cart = cartDAO.checkUserID(getMemberLoginController().getUser().getUserid());
                cartPk.setUserid(getMemberLoginController().getUser().getUserid());
                cartPk.setItemid(item.getItemid());
                cart.setCartPK(cartPk);
                cart.setCartQuantity(1);
                cart.setPrice(item.getPrice());
                cart = cartDAO.addCart(cart);

        }else {
                 cartPk.setItemid(item.getItemid());
                 cartPk.setUserid(getMemberLoginController().getUser().getUserid());
                 cart.setCartPK(cartPk);
                 cart.setCartQuantity(1);
                 cart.setPrice(item.getPrice());
                 cart = cartDAO.addCart(cart);
        }

    }

my select statement

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

my checkuseridMethod

public Cart checkUserID(int id){
        Cart cart = null;
        try {
            Query query = em.createNamedQuery("Cart.findByUserid");
            query.setParameter("userid", id);
            return (Cart) query.getSingleResult();
        } catch (NoResultException e) {
            return null;
        }
    }


Well, it seems like your database has more carts for this user (which is a plausible scenario if you store user history)

You can either make sure that there is no more than one record in the database (by deleting older carts when a new one is created, for example), or use Query.setMaxResults(1) and something like ORDER BY dateCreated in your query.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜