在通用DAO中使用Hibernate调用存储过程的最佳方式是什么? [英] What's the best way to calling a Stored Procedure using Hibernate in a Generic DAO?

查看:95
本文介绍了在通用DAO中使用Hibernate调用存储过程的最佳方式是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 调用SPGetChart(idNumber,nameChart);使用MySql查询调用如下所示: 


解决方案

使用EntityManager

 查询查询= getEntityManager()。 
createNativeQuery(BEGIN SPGetChart(:id,:name); END;);
query.setParameter(id,idValue);
query.setParameter(name,nameChart);

query.executeUpdate();

通过EntityManager使用连接:



<$ p $连接con =((SessionImpl)getEntityManager()。getDelegate())。connection();
CallableStatement callableStatement = cc.prepareCall({call SPGetChart(?,?)});

callableStatement.setInt(1,idValue);
callableStatement.setString(2,nameChart);
callableStatement.execute();

使用会话:

  Query query = session.createSQLQuery(CALL SPGetChart(:id,:name))
.setParameter(id,idValue)
.setParameter(name,nameChart );
query.executeUpdate();


I'm using MySql and my query to call is like:

call SPGetChart (idNumber, nameChart);

解决方案

Using EntityManager

   Query query=getEntityManager().
                           createNativeQuery("BEGIN SPGetChart(:id, :name); END;");
   query.setParameter("id", idValue);
   query.setParameter("name", nameChart);

   query.executeUpdate();

Using connection through EntityManager:

   Connection con = ((SessionImpl) getEntityManager().getDelegate()).connection();
   CallableStatement callableStatement = cc.prepareCall("{call SPGetChart (?,?)}");

   callableStatement.setInt(1, idValue);
   callableStatement.setString(2, nameChart);
   callableStatement.execute();

Using Session:

Query query = session.createSQLQuery("CALL SPGetChart (:id, :name)")
               .setParameter("id", idValue)
                   .setParameter("name", nameChart);
query.executeUpdate();

这篇关于在通用DAO中使用Hibernate调用存储过程的最佳方式是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆