目前我有以下课程:
带有属性ID,标题和正文的类文章 课堂问题:带有额外的发布属性的文章
class Article with properties id, title and body class Question : Article with an extra PostedBy property
然后,我有了一个具有上述属性的名为Article的表,以及一个名为ID的问题表,该ID的ID为外键articleID,且为PostedBy.两者都处于不同的模式
Then I have a table called Article with the above properties and a table called questions with an ID a foreign key articleID and a PostedBy. Both are in different schemas
我想知道我的映射看起来如何代表这种关系.这两个类位于不同的程序集中,我非常不愿意将Question逻辑放在Article类/映射及其程序集中.
I would like to know how are my mappings going to look to represent this relation. Both classes are in different assemblies and i would be very reluctant to put Question logic in Article class/mapping and its assembly.
推荐答案NHibernate支持三种基本的继承策略.
NHibernate supports three basic inheritance strategies.
听起来好像您正在按子类策略查找表,因为您有Article类的表,而Question子类具有其他属性的表.映射可能看起来像这样:
It sounds like you are looking for the table per subclass strategy as you have a table for your Article class and another table for the extra properties on the Question subclass. The mapping might looks something like this:
<class name="Article" table="Article"> <id name="Id" type="Int64" column="ArticleId"> <generator class="native"/> </id> <property name="Title" column="Title"/> <property name="Body" column="Body"/> ... <joined-subclass name="Question" table="Question"> <key column="ArticleId"/> <property name="PostedBy" column="PostedBy"/> ... </joined-subclass> </class>但是,这不能满足您将映射完全分开的需求.您可能具有完全独立的映射,但是这可能会带来一些副作用,如允许将Question作为普通文章而不是Question加载.使用单独的映射,Article类将如预期的那样直接进行. Question类将包含用于访问存储在Article表中的属性的联接.
However, this doesn't meet your desire to keep the mappings entirely separate. You could have entirely separate mappings, but this might have some side effects as allowing Question to be loaded as a plain Article instead of a Question. With separate mapping the Article class would be straight-forward as expected. The Question class would include a join to access the properties stored in the Article table.
<class name="Article" table="Article"> <id name="Id" type="Int64" column="ArticleId"> <generator class="native"/> </id> <property name="Title" column="Title"/> <property name="Body" column="Body"/> ... </class> <class name="Question" table="Question"> <id name="Id" type="Int64" column="QuestionId"> <generator class="native"/> </id> <property name="PostedBy" column="PostedBy"/> ... <join table="Article"> <key column="ArticleId"/> <property name="Title" column="Title"/> <property name="Body" column="Body"/> </join> </class>更多推荐
NHibernate继承问题
发布评论