2010-10-11 14 views
3

J'ai le suivant hql. Cela fonctionne très bien si je n'essaie pas d'inclure l'entité "OrderItem". Je comprends qu'il n'y a pas de clause "on" dans hql. Quelle est la meilleure façon de rejoindre orderItemhql join - Chemin attendu pour la jointure

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); 

Voici le sql je souhaite atteindre.

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 

Répondre

0

Essayez d'utiliser un where au lieu d'un 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

Cela ne fonctionnera pas. La syntaxe de la jointure à gauche est toujours incorrecte. –

2

Pour utiliser HQL jointures gauche, vous devez cartographier les relations, en tant que chemin d'un des les tables "de" sont attendues (voir 13.3. Associations and joins)

Essayez de changer OrderItems.Product id à un bon produit many-to-one, et utiliser un right join. En guise de note, les noms de vos entités doivent être au singulier. Il semble que vous répliquiez simplement la structure de votre table en tant que classes.