简体   繁体   中英

Not able to execute remote stored procedure in MS SQL Server 2008 from Hibernate

I am trying to call remote store procedure using Hibernate. The remote sql server is MS SQL Server 2000 and the source server from where I am calling the procedure is MS SQL Server 2008. When I try to call stored procedure I am getting the following error message:

[2/29/12 16:44:57:971 MST] 00000016 SystemOut O 2012-02-29 16:44:57,971 WARN [org.hibernate.util.JDBCExceptionReporter] - SQL Error: 7213, SQLState: S0001
[2/29/12 16:44:57:971 MST] 00000016 SystemOut O 2012-02-29 16:44:57,971 ERROR [org.hibernate.util.JDBCExceptionReporter] - The attempt by the provider to pass remote stored procedure parameters to remote server 'EDM-SNEC-DBA3' failed. Verify that the number of parameters, the order, and the values passed are correct.
[2/29/12 16:44:57:971 MST] 00000016 SystemOut O 2012-02-29 16:44:57,971 INFO [ca.ab.gov.acys.c3p.publicweb.controller.ChildParticipationEdit] - Caught exception while executing 
SQLQuery: org.hibernate.exception.SQLGrammarException: could not execute query
SQLQuery sqlQuery = this.hibernateSession.createSQLQuery(" { call [dbo].[dc_C3P_get_child_id] " + "(:surname, :firstname, :birth_dt, :facility_id, :effective_dt, :child_id) }");`

I am passing all required params to stored procedure which is as follows:

CREATE  PROCEDURE dbo.dc_C3P_get_child_id
(
    @surname       char(25),
    @firstname     char(20),
    @birth_dt      datetime,
    @facility_id   numeric(8),
    @effective_dt  datetime,
    @child_id      numeric(8)  OUTPUT
)

Any help will be highly appreciated.

Thanks in advance.

Ok since you have an Out parameter in the stored procedure remember these rules for Hibernate.

  1. Only the first parameter of a stored procedure can be an OUT parameter.
  2. You cannot have more than one OUT parameter and it must be the first parameter.
  3. There are two ways to map your Java objects to the Stored Procedure results. One is where you map in an XML file one by one. the other is if the return result is the same structure of a table, then you can use the Java Object that is mapped to that table.

Also from the Hibernate website

You cannot use stored procedures with Hibernate unless you follow some procedure/function rules. If they do not follow those rules they are not usable with Hibernate. If you still want to use these procedures you have to execute them via session.connection(). The rules are different for each database, since database vendors have different stored procedure semantics/syntax.

Stored procedure queries cannot be paged with setFirstResult()/setMaxResults().

The recommended call form is standard SQL92: { ? = call functionName() } or { ? = call procedureName(}. Native call syntax is not supported.

For Oracle the following rules apply:

A function must return a result set. The first parameter of a procedure must be an OUT that returns a result set. This is done by using a SYS_REFCURSOR type in Oracle 9 or 10. In Oracle you need to define a REF CURSOR type. See Oracle literature for further information.

For Sybase or MS SQL server the following rules apply:

The procedure must return a result set. Note that since these servers can return multiple result sets and update counts, Hibernate will iterate the results and take the first result that is a result set as its return value. Everything else will be discarded.

If you can enable SET NOCOUNT ON in your procedure it will probably be more efficient, but this is not a requirement.

SO basically you can try getting the JDBC connection from Hibernate using the session.connection and then write a simple JDBC callable statement to achieve your purpose

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM