开发者

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=开发者_C百科"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!


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" />
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜