http://www.mkyong.com/hibernate/how-to-call-store-procedure-in-hibernate/
In this tutorial, you will learn how to call a store procedure in Hibernate.
MySQL store procedure
Here’s a MySQL store procedure, which accept a stock code parameter and return the related stock data.
SQL
DELIMITER $$
CREATE PROCEDURE `GetStocks`(int_stockcode varchar(20))
BEGIN
SELECT * FROM stock where stock_code = int_stockcode;
END $$
DELIMITER ;
In MySQL, you can simple call it with a call keyword :
SQL
CALL GetStocks('7277');
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.
Java
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.getStockCode());
}
2. NamedNativeQuery in annotation
Declare your store procedure inside the @NamedNativeQueries annotation.
Java
//Stock.java
...
@NamedNativeQueries({
@NamedNativeQuery(
name = "callStockStoreProcedure",
query = "CALL GetStocks(:stockCode)",
resultClass = Stock.class
)
})
@Entity
@Table(name = "stock")
public class Stock implements java.io.Serializable {
...
Call it with getNamedQuery().
Java
Query query = session.getNamedQuery("callStockStoreProcedure")
.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.getStockCode());
}
3. sql-query in XML mapping file
Declare your store procedure inside the “sql-query” tag.
Markup
<!-- Stock.hbm.xml -->
...
<hibernate-mapping>
<class name="com.mkyong.common.Stock" table="stock" ...>
<id name="stockId" type="java.lang.Integer">
<column name="STOCK_ID" />
<generator class="identity" />
</id>
<property name="stockCode" type="string">
<column name="STOCK_CODE" length="10" not-null="true" unique="true" />
</property>
...
</class>
<sql-query name="callStockStoreProcedure">
<return alias="stock" class="com.mkyong.common.Stock"/>
<![CDATA[CALL GetStocks(:stockCode)]]>
</sql-query>
</hibernate-mapping>
Call it with getNamedQuery().
Java
Query query = session.getNamedQuery("callStockStoreProcedure")
.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.getStockCode());
}
Conclusion
The above three approaches are doing the same thing, call a store procedure in database. There are not much big different between the three approaches, which method you choose is depend on your personal prefer.
Read More:
- How to debug stored procedure/function in TOAD
- DB2, create stored procedure error, sqlcode = – 104, sqlstate = 42601, PSM_ semicolon
- DB2 timed task execution stored procedure cannot complete scheduler thread’s initialization
- Runtime error 5 Invalid procedure call or argument
- How to set the custom blood bar UI in UE4 and how to call it
- How to Fix error performing isolated work; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarE
- Clear the user name and password stored in GIT
- How to Use qdbusinterface to call DBUS interface
- PHP Fatal error: Call to a member function query() on a non-object in
- Fatal error: Call to a member function bind_param() on a non-object in
- When the mybatis field contains an expression, an error is reported when it is stored in the database
- Python how does a. Py file call classes and functions in another. Py file
- nested exception is org.hibernate.PropertyAccessException: Null value was assigned to a property
- SQL error: 156, sqlstate: S1000 error encountered in Hibernate
- The solution of Hibernate query returning all null lists
- R ggplot Error in .Call.graphics(C_palette2, .Call(C_palette2, NULL)): invalid graphics state
- Solution to the error code of 0x80040154 in COM / ole call
- Missing parents in call to ‘print’
- SQL Server calls Database Mail to send mail error: Msg 229, Level 14, State 5, Procedure sp_send_dbmail, Line 1 EXECUTE