2013-01-05 55 views
0

我有兩個表像Doctorshift通過Hibernate查詢語言,我選擇了兩個fieldsDoctor表,並在一個單一的查詢 從Shift表中的兩個領域,所以我面臨的一個問題來顯示數據。 我寫的,像DoctorShift一個bean CALSS到列表轉換成List<DoctorShift>我越來越java.lang.ClassCastException所以做什麼 給予同樣的想法.....java.lang.ClassCastException在Hibernate中

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 "; 

幫我...

在此先感謝

+0

您能否顯示您的註釋類。 –

+0

實際上,我dint配置DoctorShift類,我是新來的Hibernate,如果需要告訴我該怎麼做.. – Clarence

回答

0

我覺得你可以做你想做的事,2種方式:

1)創建DoctorShift類構造函數,它所有nece ssary參數和在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)獲取的對象[]數組來代替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 
... 

在第二種情況下您的查詢將仍然爲您發佈的相同。

0

否則,您可以使用ResultTransformer在您的bean中加載數據。可能是this blog將幫助

相關問題