使用JPA / PLay </object>从Join查询迭代List <object>

时间:2012-01-22 04:53:12

标签: java jpa playframework

这是我使用实体管理器的查询。尝试使用play框架和jpa加入2表。

 List<Object> joinQryResult = JPA.em().createNativeQuery(
         "select e.elementname as elementname, " +
         "c.comparetype as comparetype, " + 
         "jd.matchvalue as matchvalue " +
         "from details jd " +
         "join elements e on jd.elementnamerid = e.rid " +
         "join comparers c on jd.comparetyperid = c.rid " +
         "where jd.rid = " + temp.rid).getResultList();

 Not sure how to iterate and get the values from List<Object>

我试过这个

List<MyClass> myClass = (List<MyClass>)(Object)joinQryResult;


for(MyClass myC:jd)
{
 System.out.println(myC.ElementName); //intellisense shows the property here
}

MyClass定义:尝试将List转换为此类型

public class MyClass {

    public String ElementName;

    public String CompareType;

    public String MatchValue;

    public JobDetails(String ElementName, String CompareType, String MatchValue)
    {
        this.ElementName = ElementName;

        this.CompareType = CompareType;

        this.MatchValue = MatchValue;
    }
}

获取此错误

ClassCastException occured : [Ljava.lang.Object; cannot be cast to models.MyClass

1 个答案:

答案 0 :(得分:2)

查询的结果将是一个List,其中对象的元素是相应的对象类型。

您可以执行以下操作来映射到您的对象

    List<Object[]> results = query.getResultList();
    JobDetails jobDetail = null;
    for (Object[] objects : results) {
        jobDetail = new JobDetail((String) objects[0],(String) objects[1],(String) objects[2])
    }