简体   繁体   English

如何使用休眠方式调用存储过程?

[英]How to call a stored procedure using hibernate?

I am having some problems with hibernate and MySQL. 我在使用休眠和MySQL时遇到了一些问题。 I've been reading but I am getting even more confused, so I imagine you could help me understand what I should do next. 我一直在读书,但我变得更加困惑,所以我想你可以帮助我理解下一步该怎么做。

I have a MySQL database in which I added this Stored Procedure (thanks to the Stack Overflow people) 我有一个MySQL数据库,在其中添加了此存储过程(感谢Stack Overflow的人们)

CREATE PROCEDURE BookBed (
OUT oReservaOK boolean,
pPaciente varchar(255),
pHospital bigint(20))
BEGIN
 DECLARE NumLeitosDisponiveis INT;
 DECLARE dt TIMESTAMP;

SET dt = (Select now());
SET NumLeitosDisponiveis = (SELECT AVAILABLEBEDCOUNT FROM HOSPITAL WHERE ID = pHospital); 

IF((SELECT NumLeitosDisponiveis) > 0) THEN 
BEGIN
    START TRANSACTION;

    INSERT INTO RESERVATION(PERSON, HOSPITAL, DATE)
        VALUES (pPaciente, pHospital, dt);

    UPDATE HOSPITAL
        SET AVAILABLEBEDCOUNT = AVAILABLEBEDCOUNT - 1 
    WHERE ID = pHospital;

    SET oReservaOk = true;

    commit;
END;
ELSE 
    SET oReservaOk = false;     
END IF;
END

I've read somewhere that calling functions and procedures using hibernate would be very similar. 我读过某个地方,使用休眠模式调用函数和过程将非常相似。 Then I found (coincidently also in StackOverflow ), the code to execute a function using from my java application using Hibernate: 然后,我发现(巧合地也在StackOverflow中 )使用Hibernate从我的Java应用程序中执行函数的代码:

session.doWork(new Work() {
        @Override
          public void execute(Connection connection) throws SQLException {
            CallableStatement call = connection.prepareCall("{ ? = call " + functionName + "(?,?,?)  }");
            call.registerOutParameter( 1, Types.BOOLEAN ); // or whatever it is
            call.setString(2, param1);
            call.setLong(3, param2);
            call.registerOutParameter( 4, Types.BOOLEAN ); // or whatever it is
            call.execute();
            DatabaseManager.this.setResult(call.getBoolean(1)); // propagate this back to enclosing class
          }
        });

I tried to use it but I get different kinds of errors (it depends how I setup the parameters). 我尝试使用它,但是会遇到不同类型的错误(这取决于我如何设置参数)。 At the current point in time, I am getting a "No value specified for parameter 3" error because I registered an output parameter. 在当前时间点,由于注册了输出参数,因此出现“参数3未指定值”错误。 I've searched for more information but I am getting confused because the websites have different approaches and many links to the documentation are broken (links to JBoss web site). 我已经搜索了更多信息,但是由于网站使用不同的方法并且文档的许多链接断开了(指向JBoss网站的链接),我感到困惑。

I've read I have to return a cursor as the first out parameter. 我读过我必须返回一个游标作为先进先出参数。 I am searching the way to do it in MySQL. 我正在寻找在MySQL中执行此操作的方法。 (But I've read this is an indication that your design is bad) What should I do? (但是我读过这表明您的设计不正确)我应该怎么办? I am completely lost... How can I solve it? 我完全迷路了...我该如何解决? Should I change the procedure to return my value in some other way? 我应该更改程序以其他方式返回我的值吗?

Thanks, Oscar 谢谢,奥斯卡

There is a mismatch between the signature of your stored procedure and the way you call it. 存储过程的签名与调用它的方式之间不匹配。 You have to call it as "call BookBed(?, ?, ?)". 您必须将其称为“调用BookBed(?,?,?)”。 Also note that it takes only 3 parameters, not 4. 还要注意,它仅需要3个参数,而不是4个。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM