[英]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.谢谢你。
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.