在FROM子句Hibernate HQL中使用子查询

时间:2015-10-03 01:19:32

标签: hibernate hql

这是我的HQL查询,它正在抛出" 无效的令牌SELECT" 。我们如何实现它,我读到Hibernate在From Clause中不支持Subquery。

SELECT subQ.workItemId ,subQ.subject, subQ.workItemCrtdDt, subQ.workItemDueDt, subQ.workItemRcpntNm, subQ.workItemCrtrNm, subQ.lkWorkItemStatusCd,subQ.lkWorkItemPriorityCd 
                    FROM 
                    (SELECT WI FROM WorkItem WI, WorkItemRecipient WIR 
                      WHERE WI.workItemId = WIR.workItem.id and
                    (WI.workItemCreateUserId=:userId or 
                    (WIR.recipientId= :userId or (WIR.recipientId= :allUserId and WIR.recipientOrgId= :userOrgId)))) As subQ

1 个答案:

答案 0 :(得分:1)

您的查询可以简单地重写为常规选择,您只需要解决 subQ到正确的别名。

假设:

表:WorkItem包含以下列:workItemId,subject,workItemCrtdDt,workItemDueDt,workItemCrtrNm,lkWorkItemStatusCd,lkWorkItemPriorityCd

表:WorkItemRecipient的列为:workItemRcpntNm

然后查询将是:

SELECT WI.workItemId ,WI.subject, WI.workItemCrtdDt, WI.workItemDueDt, WIR.workItemRcpntNm, WI.workItemCrtrNm, WI.lkWorkItemStatusCd,WI.lkWorkItemPriorityCd 
    FROM WorkItem WI, WorkItemRecipient WIR 
    WHERE WI.workItemId = WIR.workItem.id 
    and (WI.workItemCreateUserId=:userId or  WIR.recipientId= :userId or (WIR.recipientId= :allUserId and WIR.recipientOrgId= :userOrgId)))

不需要在From子句中进行任何子选择。