Question

I am trying to achieve this query with HQL:

SELECT * 
FROM Gift g 
LEFT OUTER JOIN (SELECT det.GiftId FROM ItemDetails det WHERE det.Active=1) r1 ON g.GiftId=r1.GiftId 
WHERE r1.GiftId IS null

This query gives me what I need in SQL Server but I need it to work in HQL. I have tried this:

SELECT distinct g
FROM (SELECT det.gift FROM ItemDetails det WHERE det.active=1) as r
RIGHT OUTER JOIN r.gift g
WHERE r.gift IS null

GiftId is a foreignkey in ItemDetails that references a row in Gift. I think this is not working because the nested query is in the FROM clause but I am not sure how to reword it. Any suggestions?

EDIT: Final working version..

   SELECT distinct g
   FROM Gift g 
   WHERE NOT EXISTS (from ItemDetails det where det.active=1 and det.gift=g.id)

No correct solution

OTHER TIPS

Does this version work?

SELECT g.*
FROM Gift g
WHERE not exists (select 1
                  from ItemDetails det
                  where det.Active = 1 and
                        det.GiftId = g.GiftId
                 );

You can also do this as a left outer join:

SELECT g.* 
FROM Gift g LEFT OUTER JOIN
     ItemDetails det 
     on det.Active = 1 and g.GiftId = r1.GiftId 
WHERE det.GiftId IS null;

You don't need the subquery, because you can just move the filtering condition into the on clause.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top