代码之家  ›  专栏  ›  技术社区  ›  alastairs

NHibernate中的多对多集合映射

  •  0
  • alastairs  · 技术社区  · 16 年前

    我的应用程序是一个多用户播客聚合器,使用NHibernate 2.1(如果有什么变化,还可以使用.NET 4.0)。我也是一个完整的NHibernate n00b。

    Subscriptions(UserId, FeedId)
    

    UserFeedItems(UserId, FeedItemId, IsNew, ListenCount, etc.)
    

    class Podcast {
        IList<PodcastFeedItem> FeedItems { get; set; }
        bool HasNew {
            get {
                // return true if any of the FeedItems are new
            }
        }
    }
    
    class PodcastFeedItem {
        bool IsNew { get; set; }
    }
    
    class User {
        IList<PodcastFeed> Subscriptions { get; set; }
    }
    

    the documentation on collection mapping

    2 回复  |  直到 16 年前
        1
  •  1
  •   reach4thelasers    16 年前

    这是一个经典的“多对多”模式,一个播客可以有多个订阅者,一个订阅者可以订阅多个播客。你把它映射成这样:

    <class name="App.Core.Domain.User, App.Core" table="users">
        <set name="SubscribedPodcasts" table="subscriptions" inverse="false" cascade="all">
          <key column="userid"/>
          <many-to-many class="App.Core.Domain.Podcasts, App.Core" column="podcastid"/>
        </set>
    </class>
    
    
    <class name="App.Core.Domain.Podcast, App.Core" table="podcasts">
        <set name="SubscribedUsers" table="subscriptions" inverse="false" cascade="all">
          <key column="podcastid"/>
          <many-to-many class="App.Core.Domain.User, App.Core" column="userid"/>
        </set>
    </class>
    

    <index-many-to-many
            column="column_name"                
            class="ClassName"                   
    />
    

    到地图

        2
  •  1
  •   Community Mohan Dere    8 年前

    this question 提供了有关如何在NHibernate中执行多对多操作的详细信息

    推荐文章