我有一个使用公用表表达式(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());