2014-03-27 54 views
0

我要讓不同的查詢休眠 這裏是我的DaoImpl鮮明的錯誤java.lang.Integer中不能被強制轉換爲模擬

@SuppressWarnings("unchecked") 
    public List<ModelNilai> getListNilaiAwal(Integer page, Integer id, Integer idm) { 
     // TODO Auto-generated method stub 
     List<ModelNilai> list = new ArrayList<ModelNilai>(); 
     Disjunction dis = Restrictions.disjunction(); 

     dis.add(Restrictions.eq("mahasiswa.id", id)); 


     Criteria c = getCurrentSession().createCriteria(ModelNilai.class); 

     c.createAlias("makul","m"); 

     c.setProjection(Projections.property("id")); 
     c.setProjection(Projections.distinct(Projections.property("m.id"))); 
     c.add(dis); 

     if(page!=null){ 
      c.setFirstResult((page-1)*LISTSIZE); 
     } 

     /*Mengurutkan data yang dipilih berdasar id secara Descending*/ 
     c.addOrder(Order.desc("id")); 

     /*Menentukan jumlah baris data dari tabel yang di pilih*/ 
     c.setMaxResults(LISTSIZE); 

     list.addAll(c.list()); 

     if(page==null){ 
      setTotalPage(c); 
     } 
     return list; 
    } 

這裏我ModelNilai

@ManyToOne 
    @JoinColumn(name="id_makul") 
    private ModelMakul makul; 

ModelMakul

@OneToMany(mappedBy="makul",cascade=CascadeType.ALL) 
     @JsonIgnore 
     private Set<ModelNilai> nilai; 

當我運行,,, 它使這個查詢

Hibernate: select distinct m1_.id_makul as y0_ from tbl_nilai this_ inner join tbl_makul m1_ on this_.id_makul=m1_.id_makul where (this_.id_mhs=?) order by this_.id_nilai desc limit ? 
Hibernate: select count(*) as y0_ from tbl_nilai this_ inner join tbl_makul m1_ on this_.id_makul=m1_.id_makul where (this_.id_mhs=?) order by this_.id_nilai desc limit ? 

和錯誤發生 java.lang.ClassCastException:java.lang.Integer中不能轉換到id.ac.undip.mlearning.model.ModelNilai

我走到哪裏錯了?我認爲查詢是正確的,但爲什麼它總是給我那個錯誤? 請幫忙,,

回答

0
c.setProjection(Projections.distinct(Projections.property("m.id"))); 

告訴Hibernate返回的ID,這將是List<Integer>的列表。您正嘗試將結果添加到List<ModelNilai>

+0

謝謝,,,我知道現在有什麼錯, 你能告訴我怎麼做不同的投影嗎? – Resi

相關問題