What's the best way to calling a Stored Proced

2019-06-07 02:19发布

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

call SPGetChart (idNumber, nameChart);

2条回答
smile是对你的礼貌
2楼-- · 2019-06-07 03:11

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();
查看更多
Explosion°爆炸
3楼-- · 2019-06-07 03:18

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();
查看更多
登录 后发表回答