Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mapping multiple discriminator values to single default class in NHibernate

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!

like image 771
Rohit Agarwal Avatar asked Aug 13 '10 20:08

Rohit Agarwal


1 Answers

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" />
like image 116
Diego Mijelshon Avatar answered Nov 02 '22 17:11

Diego Mijelshon