[英]Hibernate call sql server stored procedure throws Incorrect syntax near '@P0'
the sql server stored procedure is as follows: sql server存储过程如下:
CREATE PROCEDURE SelectStaff @rankabbr varchar(14), @sex varchar(1)
AS
SELECT * FROM staff WHERE rankabbr = @rankabbr AND sex = @sex
and I can successfully run it in SSMS by我可以通过以下方式在 SSMS 中成功运行它
exec SelectStaff 'AEO', 'M';
but in eclipse, I use hibernate to call this stored procedure as follow:但是在 eclipse 中,我使用 hibernate 调用这个存储过程如下:
Session session2 = HibernateUtil.getCurrentSession();
org.hibernate.Query query = session2.createSQLQuery(" call SelectStaff(:rankabbr, :sex) ")
.setParameter("rankabbr","AEO")
.setParameter("sex", "M");
List<Object[]> results =query.list();
but error return as follow:但错误返回如下:
23:30:57,708 WARN [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (default task-7) SQL Error: 102, SQLState: S0001 23:30:57,741 ERROR [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (default task-7) Incorrect syntax near '@P0'.
23:30:57,708 WARN [org.hibernate.engine.jdbc.spi.SqlExceptionHelper](默认任务 7)SQL 错误:102,SQLState:S0001 23:30:57,741 错误 [org.hibernate.engine.jdbc.spi。 SqlExceptionHelper](默认任务 7)'@P0' 附近的语法不正确。
what is the reason?是什么原因? I have tried to add { } in the call as
我试图在通话中添加 { } 作为
org.hibernate.Query query = session2.createSQLQuery("{ call SelectStaff(:rankabbr, :sex) }")
.setParameter("rankabbr","AEO")
.setParameter("sex", "M");
but the error is the same.但错误是一样的。
I try to replace SQLServerDialect with SQLServer2012Dialect but the same error.我尝试用 SQLServer2012Dialect 替换 SQLServerDialect 但同样的错误。
I find the following code is work:我发现以下代码有效:
Session session2 = HibernateUtil.getCurrentSession();
ProcedureCall call = session2.createStoredProcedureCall("SelectStaff");
call.registerParameter( 1, String.class, ParameterMode.IN).bindValue("AEO");
call.registerParameter( 2, String.class, ParameterMode.IN).bindValue("M");
Output output = call.getOutputs().getCurrent();
List<Object[]> results;
if (output.isResultSet()) {
results = ( (ResultSetOutput) output ).getResultList();
for (Object[] row:results) {
// ......
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.