views:

98

answers:

1

I have an existing RoleType table with data. I am trying to map this table in NHibernate using Table per class hierarchy:

<class name="IRoleType" table="RoleType">

  <id name="Id" column="RoleID">
    <generator class="native" />
  </id>

  <discriminator column="RoleID" />    

  <property name="Description" column="Description" />
  <!-- ... more properties ... -->

  <subclass name="RoleA" discriminator-value="1" />
  <subclass name="RoleB" discriminator-value="4" />
  <subclass name="RoleC" discriminator-value="7" />
</class>

Here, IRoleType is an interface, with implementors RoleA, RoleB and RoleC. This works. But here's the problem -

The table contains rows with "extra" discriminator values (2,3,5,6) that are not mapped to a persistent class. These values are deprecated in the domain, so its not useful to create persistent class for each. But we also cannot delete them from the database.

Is there a way to map these extra rows to a single "default" class? If not, how else can I solve this problem?

Thanks!

+2  A: 

You can do this by mapping those values to a single one. Example:

<discriminator
    formula="case when RoleID in (2,3,5,6) then 0 else RoleId end" />    
<subclass name="RoleA" discriminator-value="1" />
<subclass name="RoleB" discriminator-value="4" />
<subclass name="RoleC" discriminator-value="7" />
<subclass name="DefaultRole" discriminator-value="0" />
Diego Mijelshon