【问题标题】:generating reports in jpa在 jpa 中生成报告
【发布时间】:2014-04-20 08:09:22
【问题描述】:

我正在学习 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

【问题讨论】:

  • 你有一个 Teacher 实体,对吧?
  • 如果您知道对象是字符串,为什么不能将对象转换为字符串?这就是演员阵容的全部意义所在。为什么要将 List 变成 Object[][] 而不是简单地遍历列表?
  • @JBNizet,我没有使用 List 检索列表,这是我的错误。谢谢你的回复先生:)

标签: java jpa jpa-2.0 jpql


【解决方案1】:

试试这个。

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());
    }
}

【讨论】:

    猜你喜欢
    • 2011-03-02
    • 1970-01-01
    • 2023-03-21
    • 2011-04-21
    • 1970-01-01
    • 2016-12-12
    • 2023-03-17
    • 1970-01-01
    相关资源
    最近更新 更多