java 休眠 createStoredProcedureQuery 执行抛出 callablestatement.getmoreresults
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44831742/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
hibernate createStoredProcedureQuery execute throws callablestatement.getmoreresults
提问by Neil Hill
First time I've used Java to call an SP so this is probably going to be a simple issues I'm overlooking.
我第一次使用 Java 调用 SP,所以这可能是我忽略的一个简单问题。
I've got a couple of ways i'm trying to do this and am getting errors on both.
我有几种方法可以尝试执行此操作,但两者都出现错误。
em is an EntityManager that's declared higher up the class, i've inspected it during debugging and the database connection is correct.
em 是一个 EntityManager,它在类的上层声明,我在调试期间检查过它并且数据库连接是正确的。
The first is using a StoredProcedureQuery which throws the error;
第一个是使用抛出错误的 StoredProcedureQuery;
javax.persistence.PersistenceException: org.hibernate.SQLGrammarException: Error calling CallableStatement.getMoreResults
javax.persistence.PersistenceException: org.hibernate.SQLGrammarException: 调用 CallableStatement.getMoreResults 时出错
try {
StoredProcedureQuery query = em.createStoredProcedureQuery("usp_myProc");
query.registerStoredProcedureParameter(0, Integer.class, ParameterMode.IN);
query.setParameter(0, id);
query.execute();
}
catch (Exception ex) {
log.error(ex.toString());
}
My second attempt was to try and use a native query (which sounded right when I read the documentation)
我的第二次尝试是尝试使用本机查询(当我阅读文档时听起来不错)
try {
Query query1 = em.createNativeQuery("EXEC usp_myProc ?");
query1.setParameter(1, id);
Integer val = (Integer) query1.getSingleResult();
}
catch (Exception ex) {
log.error(ex.toString());
}
The Procedure has been stripped down to a simple 'Select 1' to make sure that's not a problem.
该程序已被简化为简单的“选择 1”以确保这不是问题。
PROCEDURE [usp_myProc]
-- Add the parameters for the stored procedure here
@id INT
AS
BEGIN
SELECT 1
END
I've had a google around (which is where I get the Native Query code from) but couldn't get anything to return. Even just a stored procedure call that doesn't return a value would be a god result for me now.
我有一个谷歌(这是我从中获取本机查询代码的地方),但无法返回任何东西。即使只是一个不返回值的存储过程调用现在对我来说也是一个上帝的结果。
Thanks for any help you can give.
谢谢你提供的所有帮助。
回答by Reimeus
StoredProcedureQuery
indices start at 1
StoredProcedureQuery
指数开始于 1
query.registerStoredProcedureParameter(1, Integer.class, ParameterMode.IN);
query.setParameter(1, id);