2015-10-01 26 views
0

在我的MySQL數據庫我有一個表,命名爲: - file_upload 表結構; -春節冬眠MySQL不能獲取數據

我已經存儲在這個table..Now圖片我想取圖像.. 所以,我有: -

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

我DAO類是: -

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

但我GETT ing error:---

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

爲什麼??問題出在哪裏?

回答

2

這個錯誤表示你的查詢返回4行,此用戶具有4行的file_upload表,以便query.uniqueResult()不返回唯一結果,從而改變query.uniqueResult()query.List()

使用query.uniqueResult()當你保證100%您的查詢將返回只有一個單一的結果

我不知道,如果你需要得到所有與該用戶ID或不相關的ProfilePic對象,但你可以得到的列表,然後選擇你想

的一個
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(); 
     } 
+0

Yaah ...我明白了..謝謝.. – Salini