Inheritance mapping with concrete base class

Riccardo Gregori's Avatar

Riccardo Gregori

04 Jun, 2010 07:40 PM via web

Hi,
in my domain, i have the following entities:

public class User {

public virtual int Id { get; set; }
public virtual string Name { get; set; }

}

public class Employee : User {

public virtual int EmployeeNo { get; set; }

}

both Users and employees are stored in the same table. The EmployeeNo column is a nullable int.
How can i configure FluentNHibernate mappings to create an instance of User when the EmployeeNo column is null, and an instance of Employee when EmployeeNo is not null?

Using standard .hbm.xml mapping files, i achieved that purpose using the following configuration:


<id name="Id">
    <type name="Assigned" />
</id>
<discriminator formula="case when EmployeeNo is not null then 'E' else 'U' end" type="String" />
<property name="Name" column="Name" />
<subclass name="Employee" discriminator-value="E">
    <property name="EmployeeNo" />
</subclass>

Thank you
Riccardo

  1. 2 Posted by Riccardo Gregori on 04 Jun, 2010 07:46 PM

    Riccardo Gregori's Avatar

    A part of the .hbm.xml file has been cut away...

    <class name="User" table="aut_Users" discriminator-value="U" >
        <id name="Id">`
            < type name="Assigned" />`
        </id>
        <discriminator formula="case when EmployeeNo is not null then 'E' else 'U' end" type="String" />
        <property name="Name" column="Name" />
        <subclass name="Employee" discriminator-value="E">
            <property name="EmployeeNo" />
        </subclass>
    </class >
    

Reply to this discussion

Preview Comments are parsed with Markdown. Help with syntax

Attached Files

    You can attach files up to 10MB

    What is five times five?