0
 Session session2 = HibernateUtil.getSessionFactory().openSession();
        Transaction tx2 = session2.beginTransaction();

      Query q=session2.createQuery("from studBean1 group by SNo");
        List<Student_change> l1=(List<Student_change>)q.list();
        //Student_change sc=new Student_change();



       for(Student_change sc3:l1){
           session2.save(sc3);
           tx2.commit();
           session2.close();

这里来自 studBean1 的数据我可以将数据访问到 Student_change

但它给出了不能从studbean1to 转换的 excptionStudent_change

4

2 回答 2

1

鉴于您已建模Student_change为 的泛化studBean1,aList<studBean1>不是 的子类型List<Student_change>。尝试获取 a List<studBean1>,对其进行迭代并在循环中执行强制转换。

为什么是列表字符串不是列表对象的子类型

无论如何,您的代码中还有更多问题需要修改(请参阅对您问题的评论)。

于 2013-07-03T15:32:54.503 回答
0

' query.list ' returns you a list of Type Object unless you use it with generics, in you current code you are returning a list of Object and trying to Cast it to List of Student

i believe casting the objects one by one should do

Session session2 = HibernateUtil.getSessionFactory().openSession();
Transaction tx2 = session2.beginTransaction();

Query q=session2.createQuery("from studBean1 group by SNo");
List<Object> l1=Listq.list();
// Student_change sc=new Student_change();

for(Object obj:l1){
  Student_change sc3 = (Student_Change)obj;
  session2.save(sc3);
}

tx2.commit();
session2.close();
于 2013-07-03T15:32:43.770 回答