简体   繁体   English

在mysql中调用存储过程时出错(JAVA休眠)

[英]Error when calling the Stored procedure in mysql(JAVA hibernate)

I have a strange problem that i can call the MySQL stored procedure successfully. 我有一个奇怪的问题,我可以成功调用MySQL存储过程。 But calling it using Java returns an error. 但是使用Java调用会返回错误。

Following is my Controller method: 以下是我的Controller方法:

public double getBalance(String number) {
    double balance ;
    try {
        Query query = getSession().createSQLQuery("CALL getBalance(:string_number)").addEntity(Wallet.class)
                .setParameter("string_number", number);

        List result = query.list();

        if (result.size() == 0) {
            balance = -1.0;
        } else {
            balance = (double) result.get(0);
        }
    } catch (Exception ex) {
        throw ex;
    }
    return balance;
}

This is my stored procedure: 这是我的存储过程:

 CREATE DEFINER=`root`@`localhost` PROCEDURE `getBalance`(IN string_number varchar(19)) BEGIN select amount from wallet where number=string_number; END 

The error is: 错误是:

SEVERE: Servlet.service() for servlet [dispatcher] in context with path 
[/DigitalWallet] threw exception [Request processing failed; nested 
exception is javax.persistence.PersistenceException: 
org.hibernate.exception.SQLGrammarException: could not execute query] with 
root cause
java.sql.SQLException: Column 'number' not found.

I have the number column and i can call the stored procedure in MySQL Workbench successfully. 我有数字列,我可以在MySQL Workbench中成功调用存储过程。

You should use the doWork() function instead, you can basically create the connection with Lambda expressions. 您应该改用doWork()函数,基本上可以使用Lambda表达式创建连接。 You can see a few examples HERE 您可以在此处看到一些示例

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

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