问题
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 java.lang.ClassCastException
error (see below).
I switched from getting the whole record result using sessionCriteria
(which worked) to only returning 3 of the table columns using getNamedQuery
.
Original Code Block
Criteria sessionCriteria = session.createCriteria(RunContainer.class);
@SuppressWarnings ("unchecked")
List<RunContainer> runContainer = sessionCriteria.list();
Named Query Code Block
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());
}
RunContainer Table Class
@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;
...
Error
java.lang.ClassCastException:
[Ljava.lang.Object; cannot be cast to com.scene7.is.qa.jorogumo.tables.RunContainer
Can anyone help me debug this? I am fairly new to Java and this is my first work related project.
Working Code from Accepted Answer
In case anyone runs into this post, the following code is what I ended up with 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);
}
回答1:
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 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];
}
[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
session.createSQLQuery("SELECT id, date, notes FROM RunContainer").addEntity(RunContainer.class);
You can refer here for more details.
回答2:
Try this:
query.setResultTransformer(Transformers.aliasToBean(RunContainer.class));
see documentation here: http://docs.jboss.org/hibernate/orm/3.2/api/org/hibernate/Query.html#setResultTransformer(org.hibernate.transform.ResultTransformer)
来源:https://stackoverflow.com/questions/15150776/cannot-cast-getnamedquery-results-to-table-class