2

召集所有 NHibernate 大师!

如果你们中的任何一个聪明人能帮助我解决以下难题,我将不胜感激:

我有一些实体描述来自各种来源的 RSS 提要,这些提要组合在一个名为 FeedList 的实体中。

我试图只选择与给定 FeedList 链接的不同 SourceFeed 实体。(即“WHERE FeedList.name = 'feedlist1'”。

我一直在玩 JoinQueryOver 一段时间,但我似乎无法弄清楚如何获得所需的结果。

实体的相关性如下:

FeedList > Feed > FeedSource

所以一个 FeedList 包含许多 Feed,每个 Feed 都属于一个 FeedSource。

这是实体的代码:

public class Feed
{
    public virtual int Id { get; set; }
    public virtual string Description { get; set; }
    public virtual FeedSource FeedSource { get; set; }
    public virtual string URL { get; set; }
}

public class FeedList
{
    public virtual int Id{get;set;}
    public virtual string Name { get; set; }
    public virtual IList<Feed> Feeds { get; set; }
}

public class FeedSource
{
    public virtual int Id { get; set; }
    public virtual string Name { get; set; }

}

我正在使用具有以下覆盖的 Fluent Automapping:

public class FeedOverride : IAutoMappingOverride<Feed>
{
    public void Override(AutoMapping<Feed> mapping)
    {
        mapping.References<FeedSource>(map => map.FeedSource).Cascade.All();   

    }
}

public class FeedListOverride : IAutoMappingOverride<FeedList>
{
    public void Override(AutoMapping<FeedList> mapping)
    {
        mapping.HasManyToMany<Feed> (map => map.Feeds).Cascade.All().Table("FeedList_Feed");

    }
}

下面是一些创建一些示例数据的代码:

 private static void CreateSampleData()
    {
        using (var session = HibernateHelper.OpenSession())
        {
            using (var transaction = session.BeginTransaction())
            {
                //Create source 1 and feeds

                FeedSource source1 = new FeedSource() { Name = "BBC" };

                IList<Feed> feedsForSource1 = new List<Feed>(){ 
                                                                    CreateFeed("Sample Feed1",source1,"http://feed1.xml"),
                                                                    CreateFeed("Sample Feed2",source1,"http://feed2.xml")
                                                                };

                FeedList feedList1 = CreateFeedList("FeedList1", feedsForSource1);




                //Create source 2 and feeds

                FeedSource source2 = new FeedSource() { Name = "Sky" };

                IList<Feed> feedsForSource2 = new List<Feed>(){ 
                                                                    CreateFeed("Sample Feed3",source2,"http://feed3.xml"),
                                                                    CreateFeed("Sample Feed4",source2,"http://feed4.xml"),
                                                                    CreateFeed("Sample Feed5",source2,"http://feed5.xml")
                                                                };

                FeedList feedList2 = CreateFeedList("FeedList2", feedsForSource2);

                session.SaveOrUpdate(feedList1);
                session.SaveOrUpdate(feedList2);


                transaction.Commit();
            }
        }
    }

我想达到什么目的?

如果我用 SQL 来描述它(非常糟糕),我希望做这样的事情:

select  distinct *
from FeedList as list
LEFT JOIN FeedList_Feed as list_feed
ON list.Id = list_feed.FeedList_id
LEFT JOIN Feed as feed
ON feed.FeedSource_id = list_feed.Feed_id
LEFT JOIN FeedSource as src
ON src.Id = list_feed.Feed_id
WHERE list.Name="a feedlist name"

非常感谢你花时间陪伴 :)

4

1 回答 1

3

好的,所以我终于为任何感兴趣的人解决了这个问题:

实体

public class Feed
{
    public virtual int Id { get; set; }
    public virtual string Description { get; set; }
    public virtual FeedSource FeedSource { get; set; }
    public virtual string URL { get; set; }
    public virtual FeedList FeedList { get; set; }
}

public class FeedList
{
    public virtual int Id{get;set;}
    public virtual string Name { get; set; }
    public virtual IList<Feed> Feeds { get; set; }
}

public class FeedSource
{
    public virtual int Id { get; set; }
    public virtual string Name { get; set; }
}

覆盖

public class FeedOverride : IAutoMappingOverride<Feed>
{
    public void Override(AutoMapping<Feed> mapping)
    {
        mapping.References<FeedList>(map => map.FeedList).Cascade.All();
        mapping.References<FeedSource>(map => map.FeedSource).Cascade.All();
    }
}

public class FeedListOverride : IAutoMappingOverride<FeedList>
{
    public void Override(AutoMapping<FeedList> mapping)
    {
        mapping.HasManyToMany<Feed>(map => map.Feeds).Cascade.All().Table("FeedList_Feed");
        //mapping.HasMany<FeedListFeed>(map => map.Feeds).Cascade.All();
    }
}

最后:

查询

session.QueryOver<FeedList>()
                    .Inner.JoinAlias(f => f.Feeds, () => feedAlias)
                    .Where(fl => fl.Name == name)
                    .Select(x => feedAlias.FeedSource)
                    .List<FeedSource>().Distinct().ToList();
于 2013-06-21T11:51:23.403 回答