NHibernate - multiple JOIN to the same table by different keys

前端 未结 2 1782
我寻月下人不归
我寻月下人不归 2021-01-19 00:16

Another NHibernate JOIN problem.

I\'m trying to join two different properties from one table by different two keys. But I can\'t get the second JOIN property.

<
2条回答
  •  旧巷少年郎
    2021-01-19 01:12

    I was lurking thru the internet all day long to find solution to same problem. What I found is thread on hibernate board. I took solution from Ashkan Aryan . So if anyone else gets headaches about solution and don't want to use views I will post my code which I am using now.

    I have to use from 1 to 12 joins on same table so creating views is to much confusing.

    private void addParagraphsQuery(DetachedCriteria sourceQuery, List paragraphsArguments)
    {
        DetachedCriteria dc;
        Conjunction conjunction = Restrictions.Conjunction();
        string alias = string.Empty;
    
        if (paragraphsArguments != null && paragraphsArguments.Count > 0)
        {
            for (int i = 0; i < paragraphsArguments.Count; i++)
            {
                alias = "p" + i.ToString();
                dc = DetachedCriteria.For().SetProjection(Projections.Id());
                dc.CreateAlias("paragraphList", alias);
                dc.Add(Restrictions.Eq(alias + ".paragraphSectionTemplate", paragraphsArguments[i].ParagraphTemplate));
                dc.Add(Restrictions.Like(alias + ".content", paragraphsArguments[i].Argument, MatchMode.Anywhere));
                conjunction.Add(Property.ForName("id").In(dc));
            }
        }
        sourceQuery.Add(conjunction);
    }
    

    Regards,

    Mariusz

提交回复
热议问题