无法将getNamedQuery结果转换为表类 [英] Cannot cast getNamedQuery results to table class
问题描述
我一直在寻找并尝试我在Google上搜索过的最近4小时的命名查询结果的教程,但没有找到可行的解决方案。当我运行下面代码的snippit时,我收到一个 java.lang.ClassCastException
错误(见下文)。
我从使用 sessionCriteria
(其工作)获取整个记录结果转换为仅使用 getNamedQuery
返回表列的3个结果, 。
$ hr
$ b
原始代码块
Criteria sessionCriteria = session.createCriteria(RunContainer.class);
@SuppressWarnings(unchecked)
List< RunContainer> runContainer = sessionCriteria.list();
命名查询代码块
查询查询= session.getNamedQuery(RunContainer.GetRunsForCalendar);
列表结果= query.list(); (int i = 0; i< results.size(); i ++){
RunContainer result =(RunContainer)results.get(i);
。
System.out.println(result.getNotes());
}
运行容器表类
@Entity
@Table(name =container)
@NamedQueries({
@NamedQuery(name = RunContainer.GetRunsForCalendar,
query =SELECT id,date,notes FROM RunContainer)
})
public class RunContainer {
@Id
@GeneratedValue
@Column(columnDefinition =INT UNSIGNED)
private Integer id;
私人日期日期;
@Column(columnDefinition =TEXT)
私人字符串笔记;
错误 p>
java.lang.ClassCastException:
[Ljava.lang.Object;不能转换为com.scene7.is.qa.jorogumo.tables.RunContainer
任何人都可以帮我调试吗?
来自已接受答案的工作代码
对于Java而言,我相当新,这是我第一个与工作有关的项目。如果有人遇到这篇文章,下面的代码就是我从接受的答案中得出的结论。
查询查询= session.getNamedQuery(RunContainer.GetRunsForCalendar);
列表< Object> containerResults = query.list();
列表< RunContainer> runContainers = new ArrayList< RunContainer>();
for(Object result:containerResults){
Object [] temp =(Object [])result;
RunContainer runContainer = new RunContainer();
runContainer.setId((Integer)temp [0]);
runContainer.setDate((Date)temp [1]);
runContainer.setNotes((String)temp [2]);
runContainers.add(runContainer);
}
SELECT子句查询多个列或实体,结果
聚合在getResultList()返回的java.util.List
中的对象数组(Object [])中。
在这里,您将它转换为导致问题的 RunContainer
。遍历列表和<从数组中获取单个字段。
List containerResults = query.List();
for(Object [] result:containerResults)
{
Integer id =(Integer)result [0];
日期日期=(日期)结果[1];
String notes =(String)result [2];
}
[注意:提供示例代码而不编译, b 您可以参阅此处 a>了解更多详情。 I have been looking and trying the tutorials I have googled for casting named query results for the last 4 hours and have not found a workable solution. When I run the snippit of code below I receive a I switched from getting the whole record result using Original Code Block Named Query Code Block RunContainer Table Class Error
Can anyone help me debug this? I am fairly new to Java and this is my first work related project. In case anyone runs into this post, the following code is what I ended up with from accepted answer.
The SELECT clause queries more than one column or entity, the results
are aggregated in an object array (Object[]) in the java.util.List
returned by getResultList( ). Here you're casting it to [Note : provided sample code without compilation, make changes accordingly] Edit: Alternatively, if the selected fields are same as the field names in the entity, you can try You can refer here for more details. 这篇关于无法将getNamedQuery结果转换为表类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
$ b 在实体中的名字,你可以尝试
session.createSQLQuery(SELECT id,date,notes FROM RunContainer)addEntity(RunContainer.class );
java.lang.ClassCastException
error (see below).sessionCriteria
(which worked) to only returning 3 of the table columns using getNamedQuery
.
Criteria sessionCriteria = session.createCriteria(RunContainer.class);
@SuppressWarnings ("unchecked")
List<RunContainer> runContainer = sessionCriteria.list();
Query query = session.getNamedQuery("RunContainer.GetRunsForCalendar");
List results = query.list();
for(int i = 0; i < results.size(); i++){
RunContainer result = (RunContainer)results.get(i);
System.out.println(result.getNotes());
}
@Entity
@Table (name = "container")
@NamedQueries ( {
@NamedQuery (name = "RunContainer.GetRunsForCalendar",
query = "SELECT id, date, notes FROM RunContainer")
})
public class RunContainer {
@Id
@GeneratedValue
@Column (columnDefinition = "INT UNSIGNED")
private Integer id;
private Date date;
@Column(columnDefinition = "TEXT")
private String notes;
...
java.lang.ClassCastException:
[Ljava.lang.Object; cannot be cast to com.scene7.is.qa.jorogumo.tables.RunContainer
Working Code from Accepted Answer
Query query = session.getNamedQuery("RunContainer.GetRunsForCalendar");
List<Object> containerResults = query.list();
List<RunContainer> runContainers = new ArrayList<RunContainer>();
for (Object result : containerResults) {
Object[] temp = (Object[]) result;
RunContainer runContainer = new RunContainer();
runContainer.setId((Integer) temp[0]);
runContainer.setDate((Date) temp[1]);
runContainer.setNotes((String) temp[2]);
runContainers.add(runContainer);
}
RunContainer
which causes problem. Iterate through the list & fetch individual fields from the array.List containerResults = query.List();
for(Object[] result : containerResults)
{
Integer id = (Integer) result[0];
Date date = (Date) result[1];
String notes = (String) result[2];
}
session.createSQLQuery("SELECT id, date, notes FROM RunContainer").addEntity(RunContainer.class);