Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Java和Hibernate从存储过程中获取返回值(标量),而不是结果表_Java_Sql Server_Hibernate_Stored Procedures_Return Value - Fatal编程技术网

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

使用Java和Hibernate从存储过程中获取返回值(标量),而不是结果表,java,sql-server,hibernate,stored-procedures,return-value,Java,Sql Server,Hibernate,Stored Procedures,Return Value,我很难使用Hibernate和Java从存储过程中获取返回值整数 我的存储过程如下所示: create proc dbo.CheckEquipmentAuthorization @ReplicaId int as declare @IDAuthType int select @IDAuthType = AuthorizationType.IDAuthorizationType from AuthorizationType inner join ReplicaAut

我很难使用Hibernate和Java从存储过程中获取返回值整数

我的存储过程如下所示:

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
query = session.createSQLQuery(
    "declare @result int; exec @result = CheckEquipmentAuthorization(:replicaId); select @result")
    .setParameter("replicaId", replicaId);
我正在尝试使用以下方法获取返回值:

    query = session.createSQLQuery(
    "exec CheckEquipmentAuthorization(:replicaId)")
    .setParameter("replicaId", replicaId);
但似乎我只能用这个方法得到一个结果表,因为我的过程没有生成结果表,我也不希望生成结果表,所以失败了

有没有办法使用createSQLQuery方法获取返回值

我正在使用Hibernate、Java和SQL Server。存储过程工作正常,我已经用sql客户机对其进行了测试


谢谢。

这似乎是一个复制品。是否映射了存储过程的标量返回类型?您是否正在转换为该返回类型?

这似乎是的重复。是否映射了存储过程的标量返回类型?您正在转换为该返回类型吗?

在存储过程中,我替换了

return @AuthIntValue

调用存储过程并将结果转换为我创建的对象的方式

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();
StoredProcessult对象:

public class StoredProcResult {
    public int retVal;

    public int getRetVal() {
        return retVal;
    }

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

在存储过程中,我替换了

return @AuthIntValue

调用存储过程并将结果转换为我创建的对象的方式

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();
StoredProcessult对象:

public class StoredProcResult {
    public int retVal;

    public int getRetVal() {
        return retVal;
    }

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

我知道这很古老,但我发现自己也面临同样的问题,并在使用MS SQL Server时找到了一个简单的解决方案。按如下方式修改原始查询:

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
query = session.createSQLQuery(
    "declare @result int; exec @result = CheckEquipmentAuthorization(:replicaId); select @result")
    .setParameter("replicaId", replicaId);

现在,返回代码为query.uniqueResult,无需修改原始存储过程。当您不能或不想修改原始信号存储过程时,这会很方便。

我知道这很旧,但我刚刚发现自己也面临同样的问题,并在使用MS SQL Server时找到了一个简单的解决方案。按如下方式修改原始查询:

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
query = session.createSQLQuery(
    "declare @result int; exec @result = CheckEquipmentAuthorization(:replicaId); select @result")
    .setParameter("replicaId", replicaId);

现在,返回代码为query.uniqueResult,无需修改原始存储过程。当您不能或不想修改原始信号存储过程时,这非常方便。

我不认为它是重复的,我正在尝试获取返回值,而不是结果表。我找不到关于这个问题的任何东西,只有在结果表上。我发现返回的值通常是结果表中的行数,我想得到的就是这个返回值。看起来很奇怪,这么琐碎的事情却很难做到。正确的名称是返回代码和结果集,我想要返回代码。您是否定义了结果类?根据链接的帖子/答案,必须有一个将存储过程返回到的类。否则,您应该从会话工厂获取jdbc连接并正常执行存储过程。我决定使用自定义表映射,而不是使用RETURN@RetVal,我将使用SELECT@RetVal作为RetVal并从该表中获取它。奇怪的是,没有快速简单的方法来获取查询的结果代码。它总是一个整数,所以为什么查询中没有getResultCode方法让我有点困惑。感谢您的帮助:我不相信它是重复的,我正在尝试获取返回值,而不是结果表。我找不到关于这个问题的任何东西,只有在结果表上。我发现返回的值通常是结果表中的行数,我想得到的就是这个返回值。看起来很奇怪,这么琐碎的事情却很难做到。正确的名称是返回代码和结果集,我想要返回代码。您是否定义了结果类?根据链接的帖子/答案,必须有一个将存储过程返回到的类。否则,您应该从会话工厂获取jdbc连接并正常执行存储过程。我决定使用自定义表映射,而不是使用RETURN@RetVal,我将使用SELECT@RetVal作为RetVal并从该表中获取它。奇怪的是,没有快速简单的方法来获取查询的结果代码。它总是一个整数,所以为什么查询中没有getResultCode方法让我有点困惑。感谢您的帮助:我在下面添加了我的最终工作解决方案,以帮助有相同问题的任何人。我在下面添加了我的最终工作解决方案,以帮助有相同问题的任何人。