[ljava. Lang. object; cannot be discarded
•
Java
I want to get the value from the database. In my case, I use list to get the value from the database, but I receive this error
Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to id.co.bni.switcherservice.model.SwitcherServiceSource at id.co.bni.switcherservice.controller.SwitcherServiceController.LoadData(SwitcherServiceController.java:48) at id.co.bni.switcherservice.controller.SwitcherServiceController.main(SwitcherServiceController.java:62)
This is my code
Query LoadSource = session_source.createQuery("select CLIENT,SERVICE,SERVICE_TYPE,PROVIDER_CODE,COUNT(*) FROM SwitcherServiceSource" +
" where TIMESTAMP between :awal and :akhir" +
" and PROVIDER_CODE is not null group by CLIENT,PROVIDER_CODE order by CLIENT,PROVIDER_CODE");
LoadSource.setParameter("awal",fromDate);
LoadSource.setParameter("akhir",toDate);
List<SwitcherServiceSource> result_source = (List<SwitcherServiceSource>) LoadSource.list();
for(SwitcherServiceSource tes : result_source){
System.out.println(tes.getSERVICE());
}
Any help will be happy:)
@Raffian, do you mean so?
List<Switcher> result = (List<Switcher>) LoadSource.list();
for(Switcher tes : result){
System.out.println(tes.getSERVICE());
}
Solution
java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to id.co.bni.switcherservice.model.SwitcherServiceSource
java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to id.co.bni.switcherservice.model.SwitcherServiceSource
The problem is
(List<SwitcherServiceSource>) LoadSource.list();
This returns a list of object arrays (object []), containing scalar values for each column in the switcherservicesource table Hibernate will use resultsetmetadata to derive the actual order and type of the returned scalar values
solution
List<Object> result = (List<Object>) LoadSource.list();
Iterator itr = result.iterator();
while(itr.hasNext()){
Object[] obj = (Object[]) itr.next();
//Now you have one array of Object for each row
String client = String.valueOf(obj[0]); // don't kNow the type of column CLIENT assuming String
Integer service = Integer.parseInt(String.valueOf(obj[1])); //SERVICE assumed as int
//same way for all obj[2],obj[3],obj[4]
}
Related links
> Using iterator
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码
