在HQL中加入多个表

时间:2013-12-24 09:37:30

标签: hibernate hql

任何人都可以帮我将此查询转换为HQL

SELECT      
  supplierOrderDetails.productID,
  supplierOrderDetails.orderQty,
  supplierOrderReceiveDetail.qtyArrived
FROM
 supplierOrder
INNER JOIN
  supplierOrderDetails
ON
  (supplierOrderDetails.supplierOrderID = supplierOrder.ID)
INNER JOIN 
    supplierOrderReceive
ON  
 (supplierOrderReceive.supplierOrderID = supplierOrder.ID)
INNER JOIN 
    supplierOrderReceiveDetail
ON  
 (supplierOrderReceiveDetail.supplierOrderReceiveID = supplierOrderReceive.ID)  
   AND 
 (supplierOrderReceiveDetail.ProductID =supplierOrderDetails.ProductID)     
WHERE supplierOrder.ID = 1

这是表关系

enter image description here

3 个答案:

答案 0 :(得分:4)

我已经为想要了解如何在HQL中加入多个表的人添加了查询

SELECT supplierOrderDetails.productID as product, supplierOrderDetails.orderQty as orderedQty,sum(supplierOrderReceiveDetail.qtyArrived) as qtyArrived 

FROM SupplierOrder as so, SupplierOrderDetails as supplierOrderDetails,  SupplierOrderReceiveDetail as supplierOrderReceiveDetail, SupplierOrderReceive as supplierOrderReceive

INNER JOIN supplierOrderDetails.supplierOrderID

INNER JOIN supplierOrderReceive.supplierOrder

INNER JOIN supplierOrderReceiveDetail.supplierOrderReceive

GROUP BY supplierOrderDetails.productID, supplierOrderDetails.orderQty

答案 1 :(得分:0)

这个链接很适合开始学习如何通过HQL加入实体。

HQL Join Reference

编辑:
对于hibernate加入实体,您必须在实体之间定义关联/映射 Hibernate使用Hibernate映射文件中定义的关系名称。

答案 2 :(得分:0)

HQL使用您映射的Entities并返回Entities。因此,根据您的hibernate(JPA)映射,您想要的HQL可以简单如下:

  来自SupplierOrder的

,其中id = 1

  • SupplierOrder是您映射的实体的类名
  • id是SupplierOrder中的属性名称(实例变量) 类
  • 我预先假定所有其他实体都映射到了 SupplierOrder类在树层次结构中最高。