简体   繁体   中英

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

I have a strange problem that i can call the MySQL stored procedure successfully. But calling it using Java returns an error.

Following is my Controller method:

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.

You should use the doWork() function instead, you can basically create the connection with Lambda expressions. You can see a few examples HERE

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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