如何将命名查询结果添加到Java类中

huangapple go评论77阅读模式
英文:

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&lt;CapFutureFteForecastDto&gt; getresults() {
	List&lt;resultsDto&gt; resultListDto = new ArrayList&lt;&gt;();
	Query query = getSession().getNamedQuery(&quot;getresults&quot;);
	List&lt;List&lt;Object&gt;&gt; result = query.list();
	List&lt;Float&gt; monthsDataList = new ArrayList&lt;&gt;();
	for(int i=0; i&lt;result.size();i++){
		List&lt;Object&gt; result1 = (List&lt;Object&gt;) result.get(i);
		for(int index = 1; index &lt; 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&lt;Float&gt; monthsData ;
	
	public List&lt;Float&gt; getMonthsData() {
		return monthsData;
	}

	public void setMonthsData(List&lt;Float&gt; 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&lt;Object&gt; result1 = (List&lt;Object&gt;) 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&lt;Object[]&gt; result = query.list();

huangapple
  • 本文由 发表于 2020年9月21日 18:06:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/63990133.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定