java EJB3,带有多个结果的 JPA 错误是从 Query.getSingleResult() 返回的

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4009640/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 04:21:33  来源:igfitidea点击:

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

javaormjpa

提问by MYE

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

我在 EJB3 中遇到了 JPA 的新问题,我的堆栈跟踪是:

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 the same time, but i don't know how to solve it? Please help me

我创建了一个用于将商品添加到购物车的命令按钮,并且我编写了一种将商品添加到数据库中的购物车的方法,我正在检查用户 ID 和商品 ID 是否为空,商品数量将添加为 1,这意味着当我有用户 ID 1 时将 itemid 1 添加到购物车中,我的方法将检查它是否存在,商品数量会将购物车表中的商品数量加 1,我可以用一件商品来完成,但是如果我将更多商品 2 添加到购物车中,则意味着我添加的商品是2 到购物车表中,接下来我将更多数量的 itemid 1 添加到 db 中,它会抛出异常多个结果。. . 我知道它不能同时有两个实例,但我不知道如何解决?请帮我

my addTocart method

我的 addTocart 方法

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

我的 checkuserid 方法

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;
        }
    }

回答by Bozho

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 dateCreatedin your query.

您可以确保数据库中的记录不超过一条(例如,通过在创建新购物车时删除旧购物车),或者在查询中使用Query.setMaxResults(1)类似ORDER BY dateCreated的内容。