简体   繁体   English

使用 Java 和 Hibernate 从存储过程中获取返回值(标量),而不是结果表

[英]Get return value (scalar), not result table, from stored procedure using Java and Hibernate

I'm having a hard time getting the return value (integer) from a stored procedure using Hibernate and Java.我很难从使用 Hibernate 和 Java 的存储过程获取返回值(整数)。

My stored procedure is as follows:我的存储过程如下:

create proc dbo.CheckEquipmentAuthorization
    @ReplicaId int
as
    declare @IDAuthType int
    select @IDAuthType = AuthorizationType.IDAuthorizationType from AuthorizationType
    inner join ReplicaAuthorization on ReplicaAuthorization.RefIDAuthorizationType = AuthorizationType.IDAuthorizationType
    inner join Replica_ReplicaAuthorization on Replica_ReplicaAuthorization.RefIDAuthorization = ReplicaAuthorization.IDAuthorization
    inner join Replica on Replica.IDReplica = Replica_ReplicaAuthorization.RefIDReplica
    where Replica.IDReplica = @ReplicaId
    and GETDATE() between ReplicaAuthorization.AuthBegin and ReplicaAuthorization.AuthEnd

    declare @AuthIntValue int
    set @AuthIntValue = 10
    if (@IDAuthType is not null)
    begin
        select @AuthIntValue = AuthorizationType.IntValue from AuthorizationType
        where AuthorizationType.IDAuthorizationType = @IDAuthType
    end
    print @AuthIntValue
    return @AuthIntValue

I'm trying to get the return value using:我正在尝试使用以下方法获取返回值:

    query = session.createSQLQuery(
    "exec CheckEquipmentAuthorization(:replicaId)")
    .setParameter("replicaId", replicaId);

But it seems I can only get a result table using this, and since no result table is generate from my procedure, nor do I want one to be, it fails.但似乎我只能使用它得到一个结果表,而且由于我的程序没有生成结果表,我也不希望生成一个结果表,所以它失败了。

Is there a way to get that returned value using that createSQLQuery() method?有没有办法使用 createSQLQuery() 方法获取返回值

I'm using Hibernate, Java and SQL Server.我正在使用 Hibernate、Java 和 SQL Server。 The stored procedure is working correctly (I have tested it with a sql client).存储过程工作正常(我已经用 sql 客户端对其进行了测试)。

Thank you.谢谢你。

This seems to be a duplicate of this .这似乎是this的副本。 Have you mapped a scalar return type of your stored procedure?您是否映射了存储过程的标量返回类型? And are you translating into that return type?你正在翻译成那种返回类型吗?

In the stored procedure, I replaced在存储过程中,我替换了

return @AuthIntValue

with

select @AuthIntValue as RetVat

The way I call the stored procedure and transform the result into an object I created.我调用存储过程并将结果转换为我创建的对象的方式。

StoredProcResult o = (StoredProcResult)session.createSQLQuery("exec CheckEquipmentAuthorization :replicaId")
.addScalar("retVal", Hibernate.INTEGER)
.setParameter("replicaId", replicaId)
.setResultTransformer(Transformers.aliasToBean(StoredProcResult.class))
.setCacheMode(CacheMode.GET)
.uniqueResult();

int xpto = o.getRetVal();

The StoredProcResult object: StoredProcResult 对象:

public class StoredProcResult {
    public int retVal;

    public int getRetVal() {
        return retVal;
    }

    public void setRetVal(int retVal) {
        this.retVal = retVal;
    }
}

I know this is very old, but I just found myself faced with the same problem and found a simple solution when working with MS SQL Server.我知道这已经很老了,但我发现自己遇到了同样的问题,并在使用 MS SQL Server 时找到了一个简单的解决方案。 Modify your original query as follows:修改您的原始查询如下:

query = session.createSQLQuery(
    "declare @result int; exec @result = CheckEquipmentAuthorization(:replicaId); select @result")
    .setParameter("replicaId", replicaId);

Now you get the return code as the query.uniqueResult() without the need to modify the original stored procedure.现在您将获得作为query.uniqueResult()的返回码,而无需修改原始存储过程。 This can be handy when you aren't able (or don't want) to modify the oriignal stored procedure.当您不能(或不想)修改原始存储过程时,这会很方便。

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

相关问题 如何获取休眠中存储过程返回的标量值 - How to get scalar value returned from stored procedure in hibernate Spring / Hibernate-在Java中不使用@Entity的情况下从存储过程获取结果 - Spring/Hibernate - Get result from Stored procedure without using @Entity in Java 如何使用Hibernate从SQL Server存储过程获取执行结果? - How to get the execution result from SQL Server stored procedure using Hibernate? 如何使用休眠方式返回存储过程的生成值 - How to return the generated value of a stored procedure using hibernate 无法通过使用hibernate的存储过程获取多个Table实体 - Unable to get multiple Table entities through Stored procedure using hibernate 如何从休眠中的存储过程中获取OUTPUT值(SQL SERVER) - How to get OUTPUT value from stored procedure in hibernate (SQL SERVER) 使用本机SQL Hibernate从存储过程中检索值 - Retrieving a value from Stored Procedure using Native SQL Hibernate 如何在setResultTransformer和标量中使用Hibernate存储过程 - How to use Hibernate stored procedure with setResultTransformer and scalar 我如何从sqlserver存储过程的return语句获取值到Java? - how i get a value from a return statement of sqlserver stored procedure to java? 从 java 调用 Postgres 存储过程,要返回的表 - Postgres stored procedure call from java, table to return
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM