我在我正在构建的CMS中拥有内容项的基类。它目前标记为抽象的,因为我只希望派生类被实例化。像BlogPost,Article,Photo等派生类被设置为nHibernate中我的ContentBase类的联接子类。在nHibernate中,我可以将抽象基类映射到集合吗?
我试图建立这个类和一个Tag类之间的多对多映射。我想在ContentBase类上有一个标签集合,并且在标签类上有一个ContentBase项目集合。
nHibernate会允许我将抽象ContentBase类映射为Tag类上的集合吗?我假设没有,因为它从数据库重新构造一个标记实体时,将无法实例化此类的任何实例。我真的不希望在Tag类中使用每种类型的内容项目集合(例如TaggedBlogPosts,TaggedArticles等)。
我这样做的全部原因是因为在逻辑上,内容项可以有很多标签,并且1个标签可以属于多个内容项。为了让nHibernate在映射表中为我管理关系,我相信我必须建立一个多对多的关联并将标记添加到ContentBase.Tags集合,然后将内容项添加到Tags.TaggedContentItems集合在nHibernate中创建映射表条目之前。
这里是我的映射,以供参考:
<class name="CMS.Core.Model.Tag,CMS.Core" table="bp_Tags">
<id column="TagName" name="TagName" type="String" unsaved-value="">
<generator class="assigned" />
</id>
<bag name="_taggedContentList" table="bp_Tags_Mappings" inverse="true" cascade="save-update" lazy="true">
<key column="TagName" />
<many-to-many class="CMS.Core.Model.ContentBase,CMS.Core" column="Target_Id" />
</bag>
</class>
<class name="CMS.Core.Model.ContentBase,CMS.Core" table="bp_Content">
<id name="Id" column="Id" type="Int32" unsaved-value="0">
<generator class="native"></generator>
</id>
<property name="SubmittedBy" column="SubmittedBy" type="string" length="256" not-null="true" />
<property name="SubmittedDate" column="SubmittedDate" type="datetime" not-null="true" />
<property name="PublishDate" column="PublishDate" type="datetime" not-null="true" />
<property name="State" column="State" type="CMS.Core.Model.ContentStates,CMS.Core" not-null="true" />
<property name="ContentType" column="ContentType" type="CMS.Core.Model.ContentTypes,CMS.Core" not-null="true" />
<bag name="_tagsList" table="bp_Tags_Mappings" lazy="false" cascade="save-update">
<key column="Target_Id" />
<many-to-many class="CMS.Core.Model.Tag,CMS.Core" column="TagName" lazy="false" />
</bag>
...
<joined-subclass name="CMS.Core.Model.BlogPost,CMS.Core" table="bp_Content_BlogPosts" >
<key column="Id" />
<property name="Body" type="string" column="Body" />
<property name="Title" type="string" column="Title" />
</joined-subclass>
...