如何在HQL中编写子联接

时间:2019-03-12 10:53:19

标签: hibernate spring-boot hql

我是休眠和HQL的新手,想将此sql转换为HQL查询 无法将带有子选择的子联接转换为休眠状态的HQL

     SELECT  m.ID, SUM(CASE WHEN pl.IN_OUT = 1
                       THEN pl.INP
                  ELSE 0 END + 
                  CASE WHEN MRL.IN_OUT = 1
                       THEN MRL.INP
                  ELSE 0 END +
                  CASE WHEN sl.IN_OUT = 1
                       THEN sl.INP
                  ELSE 0 END) INP

FROM   MATERIAL                      AS m
      LEFT JOIN (SELECT pl1.MATERIAL_ID, 
                   pl1.IN_OUT, 
                   pl1.WAREHOUSE_ID,
                   SUM(CASE WHEN PL1.IN_OUT = 1 THEN PL1.QUANTITY ELSE 0 END) 
        INP, 
                   SUM(CASE WHEN PL1.IN_OUT = 2 THEN PL1.QUANTITY ELSE 0 END) 
          OUTP 
            FROM PURCHASE_LINE pl1
            GROUP BY pl1.MATERIAL_ID, pl1.IN_OUT, pl1.WAREHOUSE_ID
      ) as pl ON PL.MATERIAL_ID = M.ID AND PL.WAREHOUSE_ID = W.ID

     WHERE m.ID = 273607     
  GROUP BY m.ID

1 个答案:

答案 0 :(得分:0)

HQL和JPQL不支持派生表。您可以将派生表转换为几个相关的子查询,但是我认为编写本机SQL查询会更好。