NHibernate继承问题 [英] NHibernate inheritance question

查看:74
本文介绍了NHibernate继承问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我有以下课程:

带有属性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.

  1. 每个类层次结构的表
  2. 每个子类的表
  3. 每个具体类别的表格

听起来好像您正在按子类策略查找表,因为您有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继承问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆