views:

163

answers:

2

In a project that I am working on, I have the following entities: Analyst, Client and Contractor. Each inherit from a base class User.

public abstract class User {
    public virtual int Id { get; set; }
    public virtual string Username { get; set; }
    public virtual string FullName { get; set; }
}

I then have the other classes inheriting from the base class as:

public class Analyst : User {
    // Empty class. There are no additional properties for an analyst.
}

public class Client : User {
    // Empty class. There are no additional properties for a client.
}

public class Contractor : User {
    public int TotalJobs { get; set; }
    public int JobsInProgress { get; set; }
}

For the above classes, I have the following table structure:

USER
----
UserId
Username
FullName
UserType (1 = Analyst, 2 = Client, 3 = Contractor)


CONTRACTOR
----------
UserId
TotalJobs
JobsInProgress

There are no tables for Analyst and Client classes.

I would like to know how I can write the NHibernate mapping file for the Contractor class. For the other classes, I have created a User mapping file and added Client and Analyst as sub-classes. How can I map the Contractor class?

+2  A: 

This is a perfect fit for the approach described in 8.1.4. Mixing table per class hierarchy with table per subclass

<subclass name="Contractor" discriminator-value=3>
  <join table="CONTRACTOR">
    <key column="UserId"/>
    <property name="TotalJobs"/>
    <property name="JobsInProgress"/>
  </join>
</subclass>
Diego Mijelshon
Perfect! This did the trick.
Husain
+1  A: 

Did you try a subclass on a subclass? http://www.javalobby.org/java/forums/t18300.html

Trent