Cannot cast getNamedQuery results to table class

时光总嘲笑我的痴心妄想 提交于 2019-12-11 05:22:51

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!