英文:
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("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());
}
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("TEST.VIEW_REPORT", ReportPojo.class)
.
Then query.getResultList()
will return a typed result which you can use.
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
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论