How can we call a stored procedure with Hibernate and JPA?

后端 未结 7 1380
时光取名叫无心
时光取名叫无心 2020-12-15 06:34

How can we call a stored procedure using Hibernate or JPA?

7条回答
  •  星月不相逢
    2020-12-15 07:15

    One way to call the stored procedure from hibernate

    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", "7277");
    List result = query.list();
    for(int i=0; i

    This works

提交回复
热议问题