[英]Getting select statement results from a stored procedure using JPA?
Using JPA, I'm calling a MS SQL Server 2008 R2 stored procedure that looks like this 使用JPA,我正在调用看起来像这样的MS SQL Server 2008 R2存储过程
procedure [dbo].[testProc]
@param1 varchar(max),
@param2 datetime
as
begin
EXEC sessionProc
DECLARE @reportData varbinary(max)
EXEC aThirdPartyProc
@reportData out,
@parameter1 = @param1,
@date = @param2
SELECT col1, col2
FROM fFunction(@reportData)
end
When trying to get the results from the select statement 尝试从select语句获取结果时
StoredProcedureQuery q = em.createNamedStoredProcedureQuery("reportData");
q.setParameter("param1", "val1");
q.setParameter("param2", new Date());
return (List<ReportData>) q.getResultList();
I get 我懂了
java.lang.IllegalStateException: Current CallableStatement ou was not a ResultSet, but getResultList was called
at org.hibernate.jpa.internal.StoredProcedureQueryImpl.getResultList(StoredProcedureQueryImpl.java:319)
How can I get the select statement results? 如何获得选择语句的结果?
Note: The JPA code works if I reduce testProc to a simple select (remove the two EXEC statements). 注意:如果我将testProc简化为一个简单选择(删除两个EXEC语句),则JPA代码有效。
Also, here's the ReportData entity class: 另外,这是ReportData实体类:
@Entity
@NamedStoredProcedureQuery(name = "reportData", procedureName = "testProc", resultClasses = ReportData.class, parameters = {
@StoredProcedureParameter(mode = ParameterMode.IN, name = "param1", type = String.class),
@StoredProcedureParameter(mode = ParameterMode.IN, name = "param2", type = Date.class)
})
public class ReportData {
@Id
private String col1;
private String col2;
// getter and setter for col1 and col2
}
I've tested the proc in SQL Server Management Studio and it works fine returning results from the select statement. 我已经在SQL Server Management Studio中测试了proc,它可以很好地从select语句返回结果。
Feedback from multiple queries within SPs throw JPA off unless SET NOCOUNT ON;
SP中来自多个查询的反馈将使JPA无效,除非将
SET NOCOUNT ON;
is used. 用来。
Insert SET NOCOUNT ON;
插入
SET NOCOUNT ON;
after BEGIN in your SP. 在您的SP中开始之后。
Using SET NOCOUNT ON
is a general best practice . 通常,最佳做法是使用
SET NOCOUNT ON
。
Introduce a parameter with mode = ParameterMode.REF_CURSOR
and let your stored procedure return refcursor. 引入一个
mode = ParameterMode.REF_CURSOR
并让您的存储过程返回refcursor。
and then use 然后使用
q.execute();
return (List<ReportData>) q.getResultList();
See here section Stored procedures with REF_CURSOR 请参阅此处 使用REF_CURSOR的存储过程
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.