AF
HomeTagSubmit NotesAsk AnythingLoginSubscribe Us
AF
1. Feel Free to ask and submit anything on Anyforum.in and get satisfactory answer
2. Registration is not compulsory, you can directly login via google or facebook
3. Our Experts are looking for yours ?.



java-hibernate: How to call procedures and functions using hibernate?

could you plz explain the concept of procedures & functions by taking an eample with sample code.

java x 210
hibernate x 23
Posted On : 2016-09-11 08:24:33.0
profile MOHAMMAD SALEEM BASHA - anyforum.in MOHAMMAD SALEEM BASHA
267150
up-rate
4
down-rate

Answers


CREATE PROCEDURE ´GetStocks´(int_stockcode varchar(20))
BEGIN
SELECT * FROM stock where stock_code = int_stockcode;
END $$

There are three approaches to call a database store procedure in Hibernate.

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", "1111");

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.

//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().

Query query = session.getNamedQuery("callStockStoreProcedure")
.setParameter("stockCode", "1111");
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.

<!-- Stock.hbm.xml -->
...
<hibernate-mapping>
<class name="in.anyforum.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="in.anyforum.Stock"/>
<![CDATA[CALL GetStocks(:stockCode)]]>
</sql-query>

</hibernate-mapping>
Call it with getNamedQuery().

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());
}

Posted On : 2016-09-13 00:16:34
Satisfied : 1 Yes  0 No
profile Rishi Kumar - anyforum.in Rishi Kumar
523188221961
Reply This Thread
up-rate
4
down-rate



Post Answer
Please Login First to Post Answer: Login login with facebook - anyforum.in