从Hibernate调用时,MySQL存储过程返回的结果集大小为零

时间:2019-06-14 08:31:03

标签: java mysql hibernate

我有一个使用公用表表达式(CTE)“ WITH”子句用Mysql编写的存储过程。该存储过程在通过Mysql工作台调用时返回有效行,但从Hibernate调用时返回零行。这是存储过程代码。

使用的Mysql版本:8.0.16 MySQL Community Server 使用的Hibernate版本是5.0

CREATE DEFINER=`sa`@`localhost` PROCEDURE `spXXXXXXXXXX`(
    -- Add the parameters for the stored procedure here
    IN testparam longtext,
    IN testCode varchar(20)
)
BEGIN

    -- format testparam '`a,b,c`' as ('a','b','c')
    if(testparam is NOT NULL AND testparam != '') then
        set testparam = REPLACE(testparam,', ',',');
        set testparam = REPLACE(testparam,'`','(''');
        set testparam = REPLACE(testparam,'`',''')');
        set testparam = REPLACE(testparam,',',''',''');
    end if;


    set @sqlQuery = concat('With CTE_Test as(select a.*,row_number() over (order by Last_Modified_Date desc )
                as row_num from XXXXXX a
                where XXXXX_Code in 
                (select XXXXX_Code FROM XXXXXXX where XXXXXXX in
                (select xxxxxx from testtbl where xxxxxxxx in (''',testparam,''')
                and active = 1 and Company_Code = ''',testCode,''')) 
                and a.active = 1 and a.Company_Code = ''',testCode,''')');

    set @sqlQuery = concat(@sqlQuery, ' select * from CTE_Test ');

     prepare query from @sqlQuery;
     EXECUTE query;
     deallocate prepare query;
END

Java代码:

Query storedProcedure = em.createNamedQuery("spXXXXXXXXX");
    storedProcedure.setParameter("testparam", testparam != null ? testparam.toString() : null);
    storedProcedure.setParameter("testCode", testCode);
    List<SpResponse> SectionList = (ArrayList<SpResponse>) storedProcedure.getResultList();
    logger.info("Result-->" + SectionList.size());

0 个答案:

没有答案