2

Using JPA, I'm calling a MS SQL Server 2008 R2 stored procedure that looks like this

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

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).

Also, here's the ReportData entity class:

@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.

2 Answers 2

10

Feedback from multiple queries within SPs throw JPA off unless SET NOCOUNT ON; is used.

Insert SET NOCOUNT ON; after BEGIN in your SP.

Using SET NOCOUNT ON is a general best practice.

0
0

Introduce a parameter with mode = ParameterMode.REF_CURSOR and let your stored procedure return refcursor.

and then use

q.execute();
return (List<ReportData>) q.getResultList();

See here section Stored procedures with REF_CURSOR

3
  • 1
    Thanks for the answer. Unfortunately, as far as I can tell, SQL Server JDBC does not support ref cursors. Please see Micrsoft's JDBC 4.2 Compliance. The "Noteworthy Implementation" column describing the supportsRefCursors method of SQLServerDatabaseMetaData contains the relevant information. I've also attempted it (with jTDS and Microsoft's driver) and I get Dialect [org.hibernate.dialect.SQLServer2008Dialect] not known to support REF_CURSOR parameters
    – James
    Oct 13, 2016 at 15:04
  • Hi @James, have you solved the problem with SQL Server drivers? Jan 11, 2018 at 14:35
  • @Rodolfo Martins - No. I worked around the issue by breaking the original stored proc into multiple stored procs. Then, on the Java side, I call each stored procs. (In my case, I needed the calls executed in a single transaction). Dkroot's answer may also work. I need to set some time aside to test.
    – James
    Jan 22, 2018 at 18:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.