What's the best way to calling a Stored Procedure using Hibernate in a Generic DAO?

依然范特西╮ 提交于 2019-12-07 09:48:28

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

I know this is a old question but for those that find this now, the EntityManager class now has support for stored procedures now.

StoredProcedureQuery query = getEntityManager().createStoredProcedureQuery("SPGetChart");

query.registerStoredProcedureParameter(1, Integer.class, ParameterMode.IN);
query.registerStoredProcedureParameter(2, String.class, ParameterMode.IN);

query.setParameter(1, idValue);
query.setParameter(2, nameChart);
query.execute();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!