gpt4 book ai didi

java - 在jpa中生成报告

转载 作者:行者123 更新时间:2023-12-01 13:07:14 25 4
gpt4 key购买 nike

我正在学习 JPA,想知道如何使用下面的查询生成报告。

Query query = em
.createQuery("SELECT t.firstName, t.lastName, t.salary FROM Teacher t");

List results = query.getResultList();
Object[] objects = results.toArray(new Object[results.size()]);
for (Object object : objects) {
System.out.println(object);// here I want to print firstName, lastName
}

我知道 getResultList() 方法返回对象数组列表。但我想在这里打印 firstNamelastNamesalary 。我怎样才能做到这一点?我不能简单地将 Object 转换为 String

最佳答案

试试这个。

List<Object[]> list = query.getResultList();
for (int index = 0; index < list.size(); index++) {
Object[] objArr = list.get(index); //this will return a row of your result into objArr
for (int j = 0; j < 3; j++) {
//here, you are now iterating through the columns in each row. Since your select query has 3 columns j <3 in the for loop
System.out.println(objArr[j].toString());
}
}

关于java - 在jpa中生成报告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23179894/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com