0

我有两个表Doctorshift 通过 Hibernate 查询语言,我在单个查询中从表中选择两个fields,从Doctor表中选择两个字段,Shift所以我面临显示该数据的问题。我为此写了一个bean类,喜欢DoctorShift将列表转换为List<DoctorShift>我得到的java.lang.ClassCastException结果,所以该做什么给出相同的想法.....

code is like this

Query qry="-----";//valid query

List<DoctorShift> list=(List<DoctorShift>)qry.list();
System.out.println(" Size of list is "+list.size());// i'm getting 2

DoctorShift ds=list.get(0);// here i'm getting Exception




qry="select s.shiftNameId,s.shiftName,d.doctorRegnumvar,"
      +"d.doctorFnamevar,d.doctorLnamevar,d.doctorstatus," 
      +"sm.shiftstatus,s.shiftTime from ShiftDaoBean s,"
      + "DoctorDaoBean d,DoctorShiftMappingBean sm where "
      +"d.doctorid=sm.doctoridfk and  s.shiftID=sm.shiftidfk"
      +" and d.doctorid=:doctorID ";

帮我...

提前致谢

4

2 回答 2

0

我认为你可以通过两种方式做你想做的事:

1) 在接受所有必要参数的 DoctorShift 类中创建构造函数,并在您的 HQL 查询中使用此构造函数创建对象:

qry="select new com.yourpackage.DoctorShift(s.shiftNameId,s.shiftName,d.doctorRegnumvar,"
      +"d.doctorFnamevar,d.doctorLnamevar,d.doctorstatus," 
      +"sm.shiftstatus,s.shiftTime) from ShiftDaoBean s,"
      + "DoctorDaoBean d,DoctorShiftMappingBean sm where "
      +"d.doctorid=sm.doctoridfk and  s.shiftID=sm.shiftidfk"
      +" and d.doctorid=:doctorID ";

2) 获取 Object[] 数组的列表,而不是 DoctorShifts 对象的列表。每个数组将代表结果集中的行。例如:

Query qry="-----";//valid query

List<Object[]> list=(List<Object[]>)qry.list();
System.out.println(" Size of list is "+list.size());// i'm getting 2

Object[] firstRow =list.get(0);

System.out.println(firstRow[0]);// will output shiftNameId
System.out.println(firstRow[1]);// will output shiftName
...

在第二种情况下,您的查询仍将与您发布的相同。

于 2013-01-05T10:13:25.270 回答
0

否则,您可以使用 ResultTransformer 在 bean 中加载数据。可能这个博客会有所帮助

于 2013-02-28T14:56:30.377 回答