我想从Hibernate调用一个存储过程,它返回一个out值。这是我的存储过程。
create procedure myProcedure
(
in in_Id int,
out out_Id int
)
begin
...
END;
我正在尝试这个来调用我的程序
Query query = session.createSQLQuery(
"CALL myProcedure(:in_Id)")
.setParameter("in_id", 123);
//Not sure how to register out parameters...??
List result = query.list();
我尝试了一切,但没有运气。你能帮我吗? 如果我尝试以上说法:
PROCEDURE myProcedure的参数数量不正确;预期2,获得1
我尝试添加像
这样的输出参数myProcedure(:out_id:in_Id)
然后它说
并未设置所有已命名的参数:
我不知道如何设置参数?它是否像以下一样?
.setParameter("out_id", ?);
感谢任何帮助:)
答案 0 :(得分:7)
最简单的方法是将out参数作为返回参数的一部分返回 (仅在您有权访问商店程序时才相关) jest添加如下所示的商店程序
create procedure myProcedure_only_in_parms (
in in_Id int)
begin
call myProcedure(in_id,@out_Id) ;
select @out_id
END;
完成后,以下列方式使用Hibernet非常简单
Query query = session.createSQLQuery(
"CALL myProcedure_only_in_parms (:in_Id)")
.setParameter("in_id", 123);
List result = query.list();
结果包含out参数,
如果你想要返回乘法参数,你可以通过这样做来添加它
选择@ parm1,@ parm2,...,@ parmn
希望它有所帮助
答案 1 :(得分:5)
我写了一篇关于how you can call MySQL stored procedures and database functions from Hibernate的非常详细的文章,但我也会在这里写一篇简短的摘要。
考虑到您有一个输出基本类型的简单存储过程:
CREATE PROCEDURE count_comments (
IN postId INT,
OUT commentCount INT
)
BEGIN
SELECT COUNT(*) INTO commentCount
FROM post_comment
WHERE post_comment.post_id = postId;
END
您可以使用JPA StoredProcedureQuery
:
StoredProcedureQuery query = entityManager
.createStoredProcedureQuery("count_comments")
.registerStoredProcedureParameter(
"postId", Long.class, ParameterMode.IN)
.registerStoredProcedureParameter(
"commentCount", Long.class, ParameterMode.OUT)
.setParameter("postId", 1L);
query.execute();
Long commentCount = (Long) query
.getOutputParameterValue("commentCount");
答案 2 :(得分:0)
示例:
会话会话= em.unwrap(Session.class); session.setHibernateFlushMode(FlushMode.MANUAL); ProcedureCall查询= session.createStoredProcedureCall(“ dbo.sp_getorderlistbyparam”,BasicResult.class); query.registerParameter(“ search_text”,String.class,ParameterMode.IN).bindValue(searchText); query.registerParameter(“ membership_nbr”,String.class, query.registerParameter(“ is_debug”,Integer.class,ParameterMode.IN).bindValue(0); query.registerParameter(“ result_count”,Integer.class,ParameterMode.OUT); basicResults =(列表)query.getResultList(); ProcedureOutputs procedureOutputs = query.getOutputs(); parameterMap.put(“ totalOrderCnt”,(Integer)procedureOutputs.getOutputParameterValue(“ result_count”));
答案 3 :(得分:0)
@PersistenceContext
private EntityManager entitymanager;
@Override
public String function(int id, int emp_id, String letype, String days, Date fromdate, Date todate, String reason,
String backup, int user_id) {
//Session session = entitymanager.unwrap(Session.class);
StoredProcedureQuery applyLeave = entitymanager.createStoredProcedureQuery("sp_apply_leave");
applyLeave.registerStoredProcedureParameter("p_id", Integer.class, ParameterMode.IN)
.registerStoredProcedureParameter("p_emp_id", Integer.class, ParameterMode.IN)
.registerStoredProcedureParameter("p_letype", String.class, ParameterMode.IN)
.registerStoredProcedureParameter("p_days", String.class, ParameterMode.IN)
.registerStoredProcedureParameter("p_fmdate", Date.class, ParameterMode.IN)
.registerStoredProcedureParameter("p_todate", Date.class, ParameterMode.IN)
.registerStoredProcedureParameter("p_reason", String.class, ParameterMode.IN)
.registerStoredProcedureParameter("p_backper", String.class, ParameterMode.IN)
.registerStoredProcedureParameter("p_user_id", Integer.class, ParameterMode.IN)
.registerStoredProcedureParameter("error_msg", String.class, ParameterMode.INOUT);
applyLeave.setParameter("p_id", id);
applyLeave.setParameter("p_emp_id" , emp_id);
applyLeave.setParameter("p_letype" , letype);
applyLeave.setParameter("p_days" , days);
applyLeave.setParameter("p_fmdate" , fromdate);
applyLeave.setParameter("p_todate" , todate);
applyLeave.setParameter("p_reason" , reason);
applyLeave.setParameter("p_backper" , backup);
applyLeave.setParameter("p_user_id" , user_id);
applyLeave.setParameter("error_msg", new String("error_msg"));
//applyLeave.executeUpdate();
String error_msg = (String) applyLeave.getOutputParameterValue("error_msg");
return error_msg;
}
在这里,我已经自动连接了entitymanager并在配置包中配置了休眠持久性
答案 4 :(得分:-1)
@Override
public String generateVoucherNumber(ExpenseHeaderDTO expenseHeaderDTO) {
Session session = sessionFactory.getCurrentSession();
ProcedureCall query = session.createStoredProcedureCall("voucher_number");
query.registerParameter(
"module", String.class, ParameterMode.IN).bindValue("EMPLOYEE_EXPENSE");
query.registerParameter(
"voucherNumber", String.class, ParameterMode.OUT);
ProcedureOutputs procedureResult=query.getOutputs();
String voucherNumber= (String) procedureResult.getOutputParameterValue("voucherNumber");
return voucherNumber;
}
参考上面的例子
答案 5 :(得分:-1)
just cal normal way but store response out param
in Object[] and we can also get multiple response value in Onject[]
Object[] respnseCode = (Object[])session.createSQLQuery(
"CALL myProcedure_only_in_parms (:in_Id)")
.setParameter("in_id", 123);
List result = query.list();
sysout("out_Id"+respnseCode[0]);
答案 6 :(得分:-3)
ResultSet rs1 = null;
ArrayList<Lookup_master> list=new ArrayList<Lookup_master>();
CallableStatement cStmt;
try {
cStmt = hibernateConfiguration.dataSource().getConnection()
.prepareCall("{call SP_ADVSER_LOOKUPMASTER(?,?,?,?,?,?,?)}");
cStmt.setString(1,lookup_master.getLookup_code());
cStmt.setString(2,lookup_master.getLookup_type());
cStmt.setString(3,lookup_master.getLookup_name());
cStmt.setString(4,lookup_master.getMeaning());
cStmt.setString(5,request.getParameter("start_date"));
cStmt.setString(6,request.getParameter("end_date"));
cStmt.registerOutParameter(7, OracleTypes.CURSOR);
cStmt.executeQuery();
rs1 = (ResultSet) cStmt.getObject(7);
while (rs1.next()) {
Lookup_master lookup_master1=new Lookup_master();
lookup_master1.setLookup_id(rs1.getInt(1));
lookup_master1.setLookup_code(rs1.getString(2));
lookup_master1.setLookup_type(rs1.getString(3));
lookup_master1.setLookup_name(rs1.getString(4));
lookup_master1.setMeaning(rs1.getString(5));
lookup_master1.setStart_date(rs1.getDate(6));
lookup_master1.setEnd_date(rs1.getDate(7));
list.add(lookup_master1);
System.out.println(lookup_master1.getLookup_id());
}
} catch (SQLException e) {
e.printStackTrace();
}
return list;
}