Muneer
Muneer

Reputation: 7564

Table per class nhibernate mapping for inheritence. How?

I went through several NHibernate mapping tutorial, Still this part seems little complicated to me. One I found in ayende's website that was explaining table per class using discriminate value in super class. In my case, I am not using a discriminate value.

How can I map this?

I have a class structure similar to the following.

enter image description here

Both, 'Student' and 'Teacher' classes are inherited from 'Person' abstract class. I need each class should be mapped to a table without using a discrimination value in super class.

Upvotes: 0

Views: 156

Answers (1)

Radim Köhler
Radim Köhler

Reputation: 123901

Assuming there are three tables: PersonTable, StudentTable, TeacherTable. We can use the

Base class mapped to PersonTable will contain common properties, and have ID generated. StudentTable and TeacherTable will be provided with the ID from a parent

<class name="Person" table="PersonTable" abstract="true">
  <id name="PID" type="Int32" column="PersonId">
    <generator class="native"/>
  </id>

  <!-- common properties of a Person -->
  <property name="FirstName" />
  <property name="LastName" />
  <property name="DOB" />
  <property name="Gender" />

    <!-- Student and its own table -->
    <joined-subclass name="Student" table="StudentTable">
      <key column="SutdentID"/> <!-- Filled with PersonId value from a base -->
      <property name="Grade" />
    </joined-subclass>

    <!-- Teacher and its own table -->
    <joined-subclass name="Teacher" table="TeacherTable">
      <key column="TeacherId"/><!-- Filled with PersonId value from a base -->
      <property name="Subjects" />
    </joined-subclass>    
</class>

And now we have tables per each super and sub class, without discriminator

Upvotes: 1

Related Questions