Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3276168
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T19:13:39+00:00 2026-05-17T19:13:39+00:00

i have new problem with JPA in EJB3 my stacktrace are: Caused by: javax.persistence.NonUniqueResultException:

  • 0

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 the 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;
        }
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T19:13:40+00:00Added an answer on May 17, 2026 at 7:13 pm

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Today i have new problem with JPA/EJB3. I have 2 table User and Group
Following on from my previous question [link text][1] , I have a new problem.
I do not have problem as such but I am quite new to Ruby.
I have a problem with JPA (EclipseLink). I am not able to delete a
im new in hibernate and JPA and i have some problems with annotations. My
I have a JPA/Spring application that uses Hibernate as the JPA provider. In one
with my RCP program I have the problem that I want to have more
Hello i have new Problem with Add Form my problem here WARNING: /test.xhtml @24,173
I have got the new problem with opening and closing form in C#. My
I'm new in Spring Framework (2.5.6) and I have Hibernate integration problem on Glassfish

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.