我正在尝试使用spring boot 1.5,hibernate 5.2,mysql
创建一个API我已经在我的模型上尝试了@NamedQuery
和@NamedNativeQuery
,但仍然不能用于长查询(见下文)。
我尝试过HQL但是一些Native SQL语法(如LIMIT)对hql不起作用。我把我的长hql查询放在jpa-named-queries.properties
文件上,这样我就可以保持我的模型干净。下面是我格式化的HQL查询(不包括select中的所有字段):
SELECT slsum.sl_brcode, loan.loantr_date, slsum.sl_clientid, slsum.sle_code, slsum.slc_code, slsum.slt_code, slsum.ref_no, (CASE WHEN slsum.slc_code = 13 THEN (SELECT arxxref_no FROM Ar ar WHERE ar.arbr_code = slsum.sl_brcode AND ar.arslc_code = slsum.slc_code AND ar.arslt_code = slsum.slt_code AND ar.arref_no = slsum.ref_no AND ar.clientidar = slsum.sl_clientid) WHEN slsum.slc_code = 12 THEN (SELECT loanxxref_no FROM Loan loan WHERE loan.loanbr_code = slsum.sl_brcode AND loan.loanslc_code = slsum.slc_code AND loan.loanslt_code = slsum.slt_code AND loan.loanref_no = slsum.ref_no AND loan.clientidloan = slsum.sl_clientid) ELSE NULL END) AS ARREFNO, slsum.tr_date, baltype.baltypeisdebit AS BaAL_TYPE (CASE WHEN coa.coaacct_type = 4 OR coa.coaacct_type = 5 OR coa.coaacct_type = 6 THEN 1 ELSE 0) END) AS SHOWTHIS, (SELECT sldtl.AMT FROM Sldtl sldtl WHERE sldtl.sl_brcode = slsum.sl_brcode AND sldtl.sl_clientid = slsum.sl_clientid AND sldtl.slc_code = slsum.slc_code AND sldtl.slt_code = slsum.slt_code AND sldtl.ref_no = slsum.ref_no AND sldtl.sle_code = slsum.sle_code AND (sldtl.adj_flag = 1 OR sldtl.adj_flag = 2) LIMIT 1) AS SETUPAMOUNT FROM Slsum slsum LEFT JOIN Glcontrol glcontrol ON glcontrol.glcontrolbr_code = slsum.sl_brcode AND glcontrol.glcontrolslc_code = slsum.slc_code AND glcontrol.glcontrolslt_code = slsum.slt_code AND glcontrol.glcontrolsle_code = slsum.sle_code LEFT JOIN Baltype baltype ON baltype.baltypeid = glcontrol.bal_type LEFT JOIN Sltype sltype ON sltype.sltypebr_code = slsum.sl_brcode AND sltype.sltypeslc_code = slsum.slc_code AND sltype.sltypeslt_code = slsum.slt_code LEFT JOIN Sletype sle_type ON sle_type.sle_typecode = slsum.sle_code AND INSTR(sle_type.sle_typeactive,CONCAT(slsum.slc_code)) <> 0 LEFT JOIN Coa coa ON coa.coabr_code = glcontrol.glcontrolbr_code AND coa.coaid = glcontrol.glcontrolacct_code LEFT JOIN Loan loan ON loan.loanbr_code = slsum.sl_brcode AND loan.loanslc_code = slsum.slc_code AND loan.loanref_no = slsum.ref_no AND loan.clientidloan = 2 AND loan.loanslt_code = slsum.slt_code WHERE slsum.sl_brcode = '2' AND slsum.sl_clientid = :clientid AND (slsum.sle_code = '11' AND (slsum.slc_code = '12' OR slsum.slc_code = '13')) GROUP BY slsum.sl_brcode, slsum.slc_code, slsum.slt_code, slsum.ref_no, slsum.sle_code
问题:如何使jpa-named-queries.properties
文件与Native SQL查询一起使用,这样我就不必将现有的长SQL查询格式化为HQL?