How to call Stored Procedure in Hibernate
MySQL store procedure Here’s a MySQL store procedure, which accept a stock code parameter and return the related stock data. DELIMITER $$ CREATE PROCEDURE ` GetStocks ` ( int_stockcode varchar ( 20 ) ) BEGIN SELECT * FROM stock where stock_code = int_stockcode ; END $$ DELIMITER ; Copy In MySQL, you can simple call it with a call keyword : CALL GetStocks ( '7277' ) ; Copy Hibernate call store procedure In Hibernate, there are three approaches to call a database store procedure. 1. Native SQL – createSQLQuery You can use createSQLQuery() to call a store procedure directly. Query query = session . createSQLQuery ( "CALL GetStocks(:stockCode)" ) . addEntity ( Stock . class ) . setParameter ( "stockCode" , "7277" ) ; List result = query . list ( ) ; for ( int i = 0 ; i < result . size ( ) ; i ++ ) { Stock stock = ( Stock ) result . get ( i ) ; System . out . println ( stock ...