简体   繁体   English

如何获取休眠中存储过程返回的标量值

[英]How to get scalar value returned from stored procedure in hibernate

I am unable to get scalar int value returned from my stored procedure. 我无法从存储过程中返回标量int值。

My stored procedure that returns scalar value 0 or -1 我的存储过程返回标量值0或-1

    CREATE PROCEDURE MY_PROC
     @ID     BIGINT
AS
     IF EXISTS ( SELECT NULL FROM USER_TABLE WHERE ID = @ID )
           RETURN 0
     ELSE
           RETURN -1
GO

Code : 代码:

    SQLQuery query = null;
    {
        query = session.createSQLQuery("{CALL MY_PROC(:ID)}");
        query.setParameter("ID", 1);            
        Object status =query.uniqueResult();
        System.out.println(status.toString()); 
     }

STACKTRACE : 堆栈跟踪 :

    WARN: SQL Error: 0, SQLState: null
Aug 24, 2015 11:15:50 AM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
ERROR: The statement did not return a result set.
org.hibernate.exception.GenericJDBCException: could not extract ResultSet
    at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:54)
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:126)
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:112)
    at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:91)
    at org.hibernate.loader.Loader.getResultSet(Loader.java:2066)
    at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1863)
    at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1839)
    at org.hibernate.loader.Loader.doQuery(Loader.java:910)
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:355)
    at org.hibernate.loader.Loader.doList(Loader.java:2554)
    at org.hibernate.loader.Loader.doList(Loader.java:2540)
    at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2370)
    at org.hibernate.loader.Loader.list(Loader.java:2365)
    at org.hibernate.loader.custom.CustomLoader.list(CustomLoader.java:353)
    at org.hibernate.internal.SessionImpl.listCustomQuery(SessionImpl.java:1909)
    at org.hibernate.internal.AbstractSessionImpl.list(AbstractSessionImpl.java:311)
    at org.hibernate.internal.SQLQueryImpl.list(SQLQueryImpl.java:141)
    at org.hibernate.internal.AbstractQueryImpl.uniqueResult(AbstractQueryImpl.java:966)
    at com.hooks.Hook.preHook(Hook.java:39)
    at com.hooks.Hook.main(Hook.java:24)
Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: The statement did not return a result set.
    at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:170)
    at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.doExecutePreparedStatement(SQLServerPreparedStatement.java:392)
    at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement$PrepStmtExecCmd.doExecute(SQLServerPreparedStatement.java:338)
    at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:4026)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:1416)
    at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeCommand(SQLServerStatement.java:185)
    at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeStatement(SQLServerStatement.java:160)
    at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.executeQuery(SQLServerPreparedStatement.java:281)
    at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:82)
    ... 16 more

According to my understanding that this procedure returns scalar value not resultset hence hibernate doesn't allow to get scalar value returned from stored procedure. 据我了解,此过程返回标量值而不是结果集,因此休眠状态不允许获取从存储过程返回的标量值。 Hibernate can only get resultSet returned from stored procedure. Hibernate只能获取从存储过程返回的resultSet。 Hibernate reference doc Hibernate参考文档

Add SET NOCOUNT ON after the AS . AS之后添加SET NOCOUNT ON Adding this will tell sql server not to return the number of modified records. 添加此内容将告诉sql server不要返回修改后的记录数。 see this question it's very useful 看到这个问题非常有用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 Java 和 Hibernate 从存储过程中获取返回值(标量),而不是结果表 - Get return value (scalar), not result table, from stored procedure using Java and Hibernate 如何在setResultTransformer和标量中使用Hibernate存储过程 - How to use Hibernate stored procedure with setResultTransformer and scalar 如何从休眠中的存储过程中获取OUTPUT值(SQL SERVER) - How to get OUTPUT value from stored procedure in hibernate (SQL SERVER) 从存储过程返回空值 - Null value is returned from stored procedure 休眠:将存储过程返回的游标转换为列表 - Hibernate: convert cursor returned from stored procedure to List 如何从Spring Jdbc中的存储过程中读取返回的值(过程中没有参数) - How read returned value from stored procedure in Spring Jdbc (without out parameters in procedure) 如何从 Hibernate 调用 MySQL 存储过程 - How to call a MySQL stored procedure from Hibernate 使用java从sql server存储过程中检索返回的值 - Retrieve the returned value from sql server stored procedure using java 使用本机SQL Hibernate从存储过程中检索值 - Retrieving a value from Stored Procedure using Native SQL Hibernate 如何将存储过程的返回值映射到Hibernate中的实体类 - How do I map return value from Stored Procedure to my entity class in Hibernate
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM