views:

161

answers:

3

Hi,

Currently I have the following classes:

class Article with properties id, title and body class Question : Article with an extra PostedBy property

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

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.

Thanks,

Yannis

+2  A: 

NHibernate supports three basic inheritance strategies.

  1. table per class hierarchy
  2. table per subclass
  3. table per concrete class

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>

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>
g .
A: 

does any of these suggestions work if the tables are on different schemas? thx

Yannis
A: 

anyone?? Is this even possible?

By posting this as a comment on the answer rather than as another answer, I'd get a notification. You also risk down votes by posting an answer that isn't an answer.
g .