0

In my mysql database I have a table,named:-file_upload table structure is ;-

I have stored picture in this table..Now I want to fetch the image.. So,I have:--

@Table(name = "file_upload")
public class ProfilePic {


      private long id;
        private String fileName;
        private byte[] data;

        --setters and getters with @Column annotation---

        public ProfilePic(BigInteger userId) {
            super();
            this.userId = userId;
        }
}

my dao class is:--

    public ProfilePic FetchImage(ProfilePic profilePic) {
String hql = "select data from  ProfilePic  where userId = :userId1 ";

        logger.info( profilePic.getUserId());

        Query query =  (Query) sessionFactory.getCurrentSession().createQuery(hql)
                .setParameter("userId1", profilePic.getUserId());


            return (ProfilePic) query.uniqueResult(); 
        }

But I am getting error:---

org.hibernate.NonUniqueResultException: query did not return a unique result: 4

why??where is the problem??

1 Answer 1

2

This error said that Your query returns 4 rows , that this user has 4 rows in your file_upload table so query.uniqueResult() doesn't return unique result so change query.uniqueResult() to query.List()

use query.uniqueResult() when you guarantee 100% that your query will return only one single result

I don't know if you need to get all the ProfilePic objects associated with that user id or not but you can get the list and choose the one you want

 public ArrayList<ProfilePic> FetchImage(ProfilePic profilePic) {
        String hql = "select data from  ProfilePic  where userId = :userId1 ";

        logger.info( profilePic.getUserId());

        Query query =  (Query) sessionFactory.getCurrentSession().createQuery(hql)
                .setParameter("userId1", profilePic.getUserId());


            return (ArrayList<ProfilePic>) query.List(); 
        }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.