gpt4 book ai didi

java - 使用 JPA/PLay 从 Join 查询迭代 List
转载 作者:行者123 更新时间:2023-12-02 08:00:42 25 4
gpt4 key购买 nike

这是我使用实体管理器的查询。尝试使用 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

最佳答案

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

您可以按照以下方式映射到您的对象

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

关于java - 使用 JPA/PLay 从 Join 查询迭代 List<Object>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8959070/

25 4 0