简体   繁体   English

如何使用JPA和Hibernate调用具有多个OUT参数的MySQL存储过程?

[英]How to call a MySQL stored procedure with multiple OUT parameters using JPA and Hibernate?

I have following Mysql stored procedure with only OUT parameters, How I can call this stored procedure through hibernate in with only OUT Parameters? 我有仅使用OUT参数的Mysql存储过程,如何通过仅使用OUT参数的休眠状态调用此存储过程?

CREATE PROCEDURE DBNAME.getStatistics (OUT A BIGINT UNSIGNED, OUT B BIGINT UNSIGNED, OUT C BIGINT UNSIGNED)
    BEGIN

        SELECT count(*) into A   from  DBNAME.Table1 where id =0;
        SELECT count(*) into B   from DBNAME.Table2 where id>0 and id<4;
        SELECT count(*) into C   from  DBNAME.Table3 where id>4;
    END

EDIT 编辑

Following code I used in java to test the call of MySQL stored procedure (with only OUT Parameters) through hibernate(Java): 我在Java中使用以下代码通过hibernate(Java)测试MySQL存储过程(仅具有OUT参数)的调用:

Session session = HibernateUtil.getSessionFactory().openSession();

Connection connection = session.connection();
CallableStatement callable = null;

try {

callable = connection.prepareCall("{Call DBNAME.getStatistics(?,?,?)}");

// three ?,?,? means three OUT parameter. 
// If any parameters is IN type of
// Lets say the first one then use : callable.setInt(1, 10);

    callable.registerOutParameter(1, Types.BIGINT);
    callable.registerOutParameter(2, Types.BIGINT);
    callable.registerOutParameter(3, Types.BIGINT);
    callable.executeUpdate();

    int value1 = callable.getInt(1);
    int value2 = callable.getInt(2);
    int value3 = callable.getInt(3);


    System.out.println(value1);
    System.out.println(value2);
    System.out.println(value3);
   } catch (SQLException e) {
       e.printStackTrace();
 }

I don't think you can do this with Hibernate. 我认为您无法使用Hibernate做到这一点。 I have seen this done before 我以前看过

Connection connection = session.connection();
CallableStatement callable = null;
callable = connection.prepareCall("execute [procedure] ?");
callable.registerOutParameter(1, Types.INTEGER);
callable.execute();
int id = callable.getInt(1);

Which has worked. 哪个有效。 See Retrieving a value from Stored Procedure using Native SQL Hibernate for more context. 有关更多上下文,请参见使用本机SQL Hibernate从存储过程中检索值

I wrote a very detailed article about how you can call MySQL stored procedures and database functions from Hibernate , but I'll write a short summary here as well. 我写了一篇非常详细的文章,介绍了如何从Hibernate调用MySQL存储过程和数据库函数 ,但是我还将在此处写一个简短的摘要。

StoredProcedureQuery query = entityManager
.createStoredProcedureQuery("getStatistics")
.registerStoredProcedureParameter("A", Long.class, ParameterMode.OUT)
.registerStoredProcedureParameter("B", Long.class, ParameterMode.OUT)
.registerStoredProcedureParameter("C", Long.class, ParameterMode.OUT);

query.execute();

Long a = (Long) query.getOutputParameterValue("A");
Long b = (Long) query.getOutputParameterValue("B");
Long c = (Long) query.getOutputParameterValue("C");

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

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