开发者

Criteria API and left join on a dictionary/map

开发者 https://www.devze.com 2023-02-20 14:28 出处:网络
I have the following classes: public class Item { public int Id { get; set; } public IDictionary<int, ItemLocal> { get; protected set; }

I have the following classes:

public class Item
{
    public int Id { get; set; }
    public IDictionary<int, ItemLocal> { get; protected set; }
    public ICollection<stri开发者_运维问答ng> Tags { get; set; }
    public int DefaultLanguageId { get; set; }
    public DateTime Start { get; set; }
}

public class ItemLocal
{
    public virtual Item Parent { get; set; }
    public virtual int LanguageId { get; set; }
    public string Title { get; set; }
    public string Description { get; set; }
}

These map to the tables Item, ItemTag and ItemLocal. I want to make the following query via the criteria api:

select
    i.Id,
    i.Start,
    l.Title
from
    Item i
    left join ItemLocal l on i.Id = l.ParentId and i.DefaultLangaugeId = l.LanguageId
order by
    l.Title,
    i.Id

But I dont know how to perform the left join with the nhibernate criteria api. Especially with the usage of the default language selection.

Any help is very appreciated.


I have found a solution:

Session
   .CreateCriteria<Item>("i")
   .CreateCriteria("Localization", "l", NHibernate.SqlCommand.JoinType.LeftOuterJoin)
      .Add(Restrictions.Disjunction()
         .Add(Restrictions.EqProperty("i.DefaultLanguageId", "l.LanguageId"))
         .Add(Restrictions.IsNull("l.LanguageId"))
      )
   .AddOrder(Order.Asc("l.Title"))
   .AddOrder(Order.Asc("w.Id"));

This seems to work but results in a query with a workaround WHERE clause. Would rather see that it could render a SQL query where the restrictions defined in the disjunction could be part of the OUTER LEFT JOIN ... ON ... statement.

0

精彩评论

暂无评论...
验证码 换一张
取 消