不同的错误java.lang.Integer不能转换为模型类

时间:2014-03-27 04:39:39

标签: hibernate distinct

我想在hibernate中进行不同的查询 这是我的DaoImpl

   @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

哪里出错了?我认为查询是正确的,但为什么它总是给我这个错误? 请帮忙,,

1 个答案:

答案 0 :(得分:0)

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

告诉hibernate返回一个id List<Integer>的列表。您正尝试将结果添加到List<ModelNilai>