如何从Hibernate调用MySQL存储过程 [英] How to call a MySQL stored procedure from Hibernate
问题描述
我试图从使用MySQL的Java应用程序调用MySQL存储过程。以下是DAO中用于调用存储过程'insertComm'的部分。
String opt =REFUND;
Query query = this.getSession()。createSQLQuery(CALL insertComm(:remitNo,:opt))
.setParameter(remitNo,remitNo)
.setParameter(opt ,opt);
opt = query.toString();
hemappLogger.info(opt);
但是,当我查询数据库并检查时,存储过程尚未执行。 'opt'值显示为
SQLQueryImpl(CALL insertComm(:remitNo,:opt))
参数没问题,应用程序也没有显示错误。我看不到我错过了什么。
不幸的是,您不能使用 Session.createSQLQuery()$ c调用存储过程$ C>。顾名思义,它允许创建一个SQL 查询。过程调用不是一个查询。
但是不要担心,解决方法就是这样。
Connection conn = getSession()。connection();
CallableStatment stat = conn.prepareCall({CALL insertComm(?,?)});
stat.setString(1,remitNo); //假设两个参数都是String
stat.setString(2,opt);
stat.executeUpdate();
stat.close();
I am trying to call a MySQL stored procedure from Java Application which uses MySQL. Below is the part in DAO where i used to call a stored procedure 'insertComm'
String opt="REFUND";
Query query = this.getSession().createSQLQuery("CALL insertComm (:remitNo, :opt)")
.setParameter("remitNo", remitNo)
.setParameter("opt", opt);
opt=query.toString();
hemappLogger.info(opt);
But as i query the database and check, the stored procedure hasn't been executed. The 'opt' value is shown as SQLQueryImpl(CALL insertComm (:remitNo, :opt))
The parameter is okay and application is not showing error also. I can't see what i missed.
Unfortunately you can't call a Stored Procedure using Session.createSQLQuery()
. As the name suggests it allows to create a SQL Query. A procedure call is not a query.
But fear not, the work around is this.
Connection conn = getSession().connection();
CallableStatment stat = conn.prepareCall("{CALL insertComm (?,?)}");
stat.setString(1, remitNo); // Assuming both parameters are String
stat.setString(2, opt);
stat.executeUpdate();
stat.close();
这篇关于如何从Hibernate调用MySQL存储过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!