[Ljava.lang.Object; 不能投射到
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20486641/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
[Ljava.lang.Object; cannot be cast to
提问by splatter_fadli
I want to get value from the database, in my case I use List
to get the value from the database but I got this error
我想从数据库中获取值,在我的情况下,我用来List
从数据库中获取值,但出现此错误
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,SERVICE,SERVICE_TYPE,PROVIDER_CODE order by CLIENT,SERVICE,SERVICE_TYPE,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 pleasure :)
任何帮助都会很高兴:)
@raffian, did you mean like this??
@raffian,你是这个意思吗??
List<Switcher> result = (List<Switcher>) LoadSource.list();
for(Switcher tes : result){
System.out.println(tes.getSERVICE());
}
采纳答案by Aniket Kulkarni
java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to id.co.bni.switcherservice.model.SwitcherServiceSource
Problem is
问题是
(List<SwitcherServiceSource>) LoadSource.list();
This will return a List of Object arrays (Object[])with scalar values for each column in the SwitcherServiceSource
table. Hibernate will use ResultSetMetadata to deduce the actual order and types of the returned scalar values.
这将返回一个对象数组列表(Object[]),其中包含SwitcherServiceSource
表中每一列的标量值。Hibernate 将使用 ResultSetMetadata 来推断返回的标量值的实际顺序和类型。
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 link
相关链接
回答by vels4j
Your query execution will return list of Object[]
.
您的查询执行将返回Object[]
.
List result_source = LoadSource.list();
for(Object[] objA : result_source) {
// read it all
}
回答by Gleb S
I've faced such an issue and dig tones of material. So, to avoid ugly iteration you can simply tune your hql:
我遇到过这样的问题并挖掘了材料的色调。因此,为了避免丑陋的迭代,您可以简单地调整您的 hql:
You need to frame your query like this
您需要像这样构建您的查询
select entity from Entity as entity where ...
select entity from Entity as entity where ...
Also check such case, it perfectly works for me:
还要检查这种情况,它非常适合我:
public List<User> findByRole(String role) {
Query query = sessionFactory.getCurrentSession().createQuery("select user from User user join user.userRoles where role_name=:role_name");
query.setString("role_name", role);
@SuppressWarnings("unchecked")
List<User> users = (List<User>) query.list();
return users;
}
So here we are extracting objectfrom query, not a bunch of fields. Also it's looks much more pretty.
所以这里我们从查询中提取对象,而不是一堆字段。而且它看起来更漂亮。
回答by Mahaveer Jangir
You need to add query.addEntity(SwitcherServiceSource.class)before calling the .list() on query.
您需要在查询时调用 .list() 之前添加query.addEntity(SwitcherServiceSource.class)。