遍历从 Hibernate 游标返回的对象列表。

huangapple go评论137阅读模式
英文:

iterating the list of objects returned from hibernate ref cursor

问题

我从Hibernate调用了一个存储过程,如下所示。Ref cursor的结果不是一个实体(entity)。结果集将是一个对象列表,包括firstName、lastName、middleName、info1、info2、address1、address2。

query = em.createStoredProcedureQuery("TEST.VIEW_REPORT")
                .registerStoredProcedureParameter("iErr", BigDecimal.class, ParameterMode.OUT)
                .registerStoredProcedureParameter("vErrMsg", String.class, ParameterMode.OUT)
                .registerStoredProcedureParameter("reportId", BigDecimal.class, ParameterMode.IN)
                .registerStoredProcedureParameter("subReportId", BigDecimal.class, ParameterMode.IN)
                .registerStoredProcedureParameter("studentId", BigDecimal.class, ParameterMode.IN)
                .registerStoredProcedureParameter("studentName", String.class, ParameterMode.IN)
                .registerStoredProcedureParameter("reportCur", void.class, ParameterMode.REF_CURSOR)
                .registerStoredProcedureParameter("headerCur", void.class, ParameterMode.OUT);

List<Object> reportResults = query.getResultList();
for(int i=0; i<reportResults.size(); i++){
     Object obj = reportResults.get(i);
     System.out.println("----"+obj.toString());
}

在这里,println输出的是[Ljava.lang.Object;@7faa080c,这又是一个包含firstName、lastName、middleName等属性的对象。

我应该如何在for循环中迭代这个listObject?获取这些值的高效方法是什么?

更新:
游标(cursor)的输出将是此查询的返回值列表,例如32行。

select
    pa.first_name as first_name,
    pa.last_name as last_name,
    pa.MIDDLE_INITIAL as middle_name,
    bi.info1 as info1,
    bi.info2 as info2,
    bi.address1 as address1,
    bi.address2 as address2
from
    studentInfo pa
英文:

I have a storedprocedure call from hibernate as below. The ref cursor result is not an entity. The result set would be list of objects like firstName, lastName, middleName, info1, info2, address1, address2.

query = em.createStoredProcedureQuery(&quot;TEST.VIEW_REPORT&quot;)
            .registerStoredProcedureParameter(&quot;iErr&quot;, BigDecimal.class,ParameterMode.OUT)
            .registerStoredProcedureParameter(&quot;vErrMsg&quot;, String.class,ParameterMode.OUT)
            .registerStoredProcedureParameter(&quot;reportId&quot;, BigDecimal.class,ParameterMode.IN)
            .registerStoredProcedureParameter(&quot;subReportId&quot;, BigDecimal.class,ParameterMode.IN)
            .registerStoredProcedureParameter(&quot;studentId&quot;, BigDecimal.class,ParameterMode.IN)
            .registerStoredProcedureParameter(&quot;studentName&quot;, String.class,ParameterMode.IN)
            .registerStoredProcedureParameter(&quot;reportCur&quot;, void.class, ParameterMode.REF_CURSOR)
            .registerStoredProcedureParameter(&quot;headerCur&quot;, void.class, ParameterMode.OUT);

List&lt;Object&gt; reportResults = query.getResultList();
for(int i=0; i&lt;reportResults.size(); i++){
     Object obj = reportResults.get(i);
     System.out.println(&quot;----&quot;+obj.toString());
}

Here println gives '[Ljava.lang.Object;@7faa080c' which is again an object with firstName, lastName, middleName..

How do I iterate this listObject in for loop? Whats the efficient way to get the values?

Update:
The output of the cursor would be a list of the return values of this query say for example 32 rows.

select
            pa.first_name as first_name,
            pa.last_name as last_name,
            pa.MIDDLE_INITIAL as middle_name,
            bi.info1 as info1,
            bi.info2 as info2,
            bi.address1 as address1,
            bi.address2 as address2
        from
            studentInfo pa

答案1

得分: 2

你应该创建一个POJO来表示存储过程返回的数据,并在createStoredProcedureQuery("TEST.VIEW_REPORT", ReportPojo.class)中提供这个类。

然后,query.getResultList()将会返回一个类型化的结果,你可以使用这个结果。

query = em.createStoredProcedureQuery("TEST.VIEW_REPORT", ReportPojo.class)
            .registerStoredProcedureParameter("iErr", BigDecimal.class, ParameterMode.OUT)
            .registerStoredProcedureParameter("vErrMsg", String.class, ParameterMode.OUT)
            .registerStoredProcedureParameter("reportId", BigDecimal.class, ParameterMode.IN)
            .registerStoredProcedureParameter("subReportId", BigDecimal.class, ParameterMode.IN)
            .registerStoredProcedureParameter("studentId", BigDecimal.class, ParameterMode.IN)
            .registerStoredProcedureParameter("studentName", String.class, ParameterMode.IN)
            .registerStoredProcedureParameter("reportCur", void.class, ParameterMode.REF_CURSOR)
            .registerStoredProcedureParameter("headerCur", void.class, ParameterMode.REF_CURSOR);

List<ReportPojo> reportResults = query.getResultList();
reportResults.forEach(System.out::println);
class ReportPojo {
    private String firstName;
    private String lastName;
    private String middleInitial;
    private String info1;
    private String info2;
    private String address1;
    private String address2;
    // getters/setters/toString
}
英文:

You should create a POJO representing the data returned by the stored procedure and provide this class in createStoredProcedureQuery(&quot;TEST.VIEW_REPORT&quot;, ReportPojo.class).

Then query.getResultList() will return a typed result which you can use.

query = em.createStoredProcedureQuery(&quot;TEST.VIEW_REPORT&quot;, ReportPojo.class)
            .registerStoredProcedureParameter(&quot;iErr&quot;, BigDecimal.class,ParameterMode.OUT)
            .registerStoredProcedureParameter(&quot;vErrMsg&quot;, String.class,ParameterMode.OUT)
            .registerStoredProcedureParameter(&quot;reportId&quot;, BigDecimal.class,ParameterMode.IN)
            .registerStoredProcedureParameter(&quot;subReportId&quot;, BigDecimal.class,ParameterMode.IN)
            .registerStoredProcedureParameter(&quot;studentId&quot;, BigDecimal.class,ParameterMode.IN)
            .registerStoredProcedureParameter(&quot;studentName&quot;, String.class,ParameterMode.IN)
            .registerStoredProcedureParameter(&quot;reportCur&quot;, void.class, ParameterMode.REF_CURSOR)
            .registerStoredProcedureParameter(&quot;headerCur&quot;, void.class, ParameterMode.REF_CURSOR);

List&lt;ReportPojo&gt; reportResults = query.getResultList();
reportResults.forEach(System.out::println);
class ReportPojo {
    private String firstName;
    private String lastName;
    private String middleInitial;
    private String info1;
    private String info2;
    private String address1;
    private String address2;
// getters/setters/toString

}

huangapple
  • 本文由 发表于 2020年10月1日 02:34:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/64143755.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定