英文:
How to add named query result to a class in java
问题
我有一个命名查询,其结果是一个列表的列表。我试图将这些数据存储到一个类(DTO)中,但在这样做时,我遇到了异常"[err] java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to java.util.List"。以下是我正在使用的代码:
public List<CapFutureFteForecastDto> getresults() {
List<resultsDto> resultListDto = new ArrayList<>();
Query query = getSession().getNamedQuery("getresults");
List<List<Object>> result = query.list();
List<Float> monthsDataList = new ArrayList<>();
for(int i=0; i<result.size(); i++){
List<Object> result1 = (List<Object>) result.get(i);
for(int index = 1; index < result1.size(); index++){
monthsDataList.add((Float)(result1.get(index)));
}
resultListDto.get(i).setName((String)result1.get(0));
resultListDto.get(i).setMonthsData(monthsDataList);
result1.clear();
monthsDataList.clear();
}
return resultListDto;
}
而我的类是:
public class resultsDto {
private String name;
private List<Float> monthsData ;
public List<Float> getMonthsData() {
return monthsData;
}
public void setMonthsData(List<Float> monthsData) {
this.monthsData = monthsData;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
命名查询的结果有3行,如下所示:
[xyz,1,24.5,1.2]
[abc,2.5,3,4]
[qwe,3,4.2,5]
我在以下代码行中遇到了" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to java.util.List" 异常:
List<Object> result1 = (List<Object>) result.get(i);
不确定我在哪里做错了。请问你能告诉我上面的代码有什么问题吗?
提前感谢您的帮助。
英文:
I have a named query which results into a list of lists. I am trying to store that data into a class(dto), while doing that i am getting an exception "[err] java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to java.util.List". below is the code i am using
public List<CapFutureFteForecastDto> getresults() {
List<resultsDto> resultListDto = new ArrayList<>();
Query query = getSession().getNamedQuery("getresults");
List<List<Object>> result = query.list();
List<Float> monthsDataList = new ArrayList<>();
for(int i=0; i<result.size();i++){
List<Object> result1 = (List<Object>) result.get(i);
for(int index = 1; index < result1.size(); index++){
monthsDataList.add((Float)(result1.get(index)));
}
resultListDto.get(i).setName((String)result1.get(0));
resultListDto.get(i).setMonthsData(monthsDataList);
result1.clear();
monthsDataList.clear();
}
return resultListDto;
}
And my class is:
public class resultsDto {
private String name;
private List<Float> monthsData ;
public List<Float> getMonthsData() {
return monthsData;
}
public void setMonthsData(List<Float> monthsData) {
this.monthsData = monthsData;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
The result of named query is having 3 rows as follows:
[xyz,1,24.5,1.2]
[abc,2.5,3,4]
[qwe,3,4.2,5]
I am getting " java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to java.util.List" exception at
List<Object> result1 = (List<Object>) result.get(i);
not sure where i am doing wrong. can you please tell me what is the issue with the above code.
Thanks in advance.
答案1
得分: 0
result.get(i)
是一个对象数组(不是列表),你需要这样声明:
Object[] result1 = (Object[]) result.get(i);
你还需要改变result
的类型:
List<Object[]> result = query.list();
英文:
result.get(i)
is an array of object (not a list), you need to declare it this way:
Object[] result1 = (Object[]) result.get(i);
You also need to change the type of result
:
List<Object[]> result = query.list();
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论