迭代List< Object>从使用JPA/PLay的Join查询中 [英] Iterate List<Object> from Join query using JPA / PLay

查看:148
本文介绍了迭代List< Object>从使用JPA/PLay的Join查询中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我使用实体管理器的查询.试图通过play框架和jpa将2个表连接起来.

This is my query using entity manager. Trying to join 2 table with play framework and jpa.

 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转换为此类型

MyClass definition: ttrying to convert List to this type

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

推荐答案

查询结果将是一个List,其对象的元素为对应的对象类型.

The result of the query will be a List with the elements of the objects being the corresponding object type.

您可以按照以下步骤映射到您的对象

You could do as follow to map to your object

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

这篇关于迭代List&lt; Object&gt;从使用JPA/PLay的Join查询中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆