mysql 如何从Hibernate调用同时具有IN和OUT参数的存储过程

fhg3lkii  于 2023-11-16  发布在  Mysql
关注(0)|答案(7)|浏览(103)

我想从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

我尝试添加一个out参数,

myProcedure(:out_id:in_Id)


但它说

未设置所有命名参数:

我不知道out参数是怎么设置的?是不是像下面这样?

.setParameter("out_id", ?);


任何帮助是赞赏:)

j1dl9f46

j1dl9f461#

假设你有一个简单的存储过程,它输出一个基本类型:

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

66bbxpm5

66bbxpm52#

最简单的方法是将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参数,如果你想返回multiply参数,你可以通过选择@parm1,@parm2,...,@parmn来添加它。
希望有帮助

5fjcxozz

5fjcxozz3#

@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;

            }

字符串
在这里,我在config包中自动连接了entitymanager并配置了hibernatePersistence

lawou6xi

lawou6xi4#

例如:

Session session = em.unwrap(Session.class);
        session.setHibernateFlushMode(FlushMode.MANUAL);
        ProcedureCall query = 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 = (List<BasicResult>) query.getResultList();
        ProcedureOutputs procedureOutputs = query.getOutputs();
        parameterMap.put("totalOrderCnt" ,(Integer) procedureOutputs.getOutputParameterValue("result_count"));

字符串

r8uurelv

r8uurelv5#

@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;
}

字符串
参考上述示例

jq6vz3qz

jq6vz3qz6#

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

字符串

wkyowqbh

wkyowqbh7#

return 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;
 }

字符串

相关问题