Hql join - expected path to join

I have the following hql. This works fine if I'm not trying to include an "OrderItem" object. I understand that in hql there is no "on" clause. What is the best way to join orderItem

 var hql = new StringBuilder();
    hql.Append(
    @"select p.Id, p.Name, p.Description, p.ProductKey, p.CustomerSku, p.ImageUrl, p.ImageThumbUrl, p.ImageLargeUrl,COUNT(oi.Quantity) as quantity 
            from ProductCampaign pc
            join pc.Product p 
            left join OrderItem oi with oi.Product.Id = p.Id
            where pc.Campaign.Id = :CampaignId ");

   hql.Append(@"group by p.Id, p.Name, P.Description, p.ProductKey, p.CustomerSku, p.ImageUrl, p.ImageThumbUrl, p.ImageLargeUrl ");
   var results = _session.CreateQuery(hql.ToString());
   results.SetParameter("CampaignId", campaignId);

Here is the sql I want to achieve.

select p.Id, p.name, p.description, p.ProductKey, p.CustomerSku, p.ImageUrl, p.ImageThumbUrl, p.ImageLargeUrl,COUNT(oi.Quantity) as quantity from ProductCampaign pc
inner join Products p on pc.ProductId = p.Id
left join orderitems oi on pc.ProductId = oi.ProductId
where pc.CampaignId = 1
group by  p.Id, p.name, p.description, p.ProductKey, p.CustomerSku, p.ImageUrl, p.ImageThumbUrl, p.ImageLargeUrl
+3
source share
2 answers

To use left HQL connections, you must match the relations, since the path from one of the "from" tables is expected (see 13.3. Associations and Unions )

Try changing the OrderItems.Productid to the appropriate Product many-to-oneand use right join.

. , .

+2

where with:

select p.Id, p.Name, p.Description, p.ProductKey, p.CustomerSku, p.ImageUrl, p.ImageThumbUrl, p.ImageLargeUrl, COUNT(oi.Quantity) as quantity 
            from ProductCampaign pc
            join pc.Product p             
            left join OrderItem oi where oi.Product.Id = p.Id 
            and pc.Campaign.Id = :CampaignId

NHibernate Non-Mapped Joins

0

Source: https://habr.com/ru/post/1768973/


All Articles