开发者

NHibernate mapping child to multiple possible parent

I have this Address table like below:

Address
-------
ID (pk)
OWNERTYPE (int)
OWNERID (int)
ADDR1 
ADDR2
....

this is a "child" table, where it would开发者_C百科 be mapped to any possible "parent" table. To discriminate which parent table it has relation to, it has the column "ONWERTYPE". "OWNERTYPE" will store the identifier which parent table it relates to, while "OWNERID" will hold the primary key of the parent table (this is Foreign key column).

how do i map this relation with the parent table?

p/s:

  • the parent table do not have any column that indicates its relation to the child.
  • the parent tables have their own entities that represent them


You need to use an <any> mapping, as explained here.


I have found another solution but I need advice whether I'm doing the correct way. The database tables itself is not design by me, thus I cant help but to follow the structure.

because all of the address data is stored only in one table, I use an inheritance mapping to do this.

for example i have 3 tables like this;

Address table

Address
-------
ID (pk)
ownertype (string)
ownerid (int)
addr1
postcode
...

User table

User
-----
ID (pk)
name
...

Org table

Org
----
ID (pk)
Orgname
....

according to my DBA, she said that both User and Org table has relation to the address table, and it is differentiated by the ownertype column in the address table.

so, I made the one base entity "Address" that has all the common properties like this:

public abstract class Address {
  public virtual string addr1 { get;set; }
  public virtual int postcode { get;set; }
  ...
}

then for each parent table relation to the address table, I made another subclass of address (derived from class Address) and made so that parent entity relationship to this subclass instead of the Address class, like so:

UserAddress subclass of Address

public class UserAddress : Address {
  public virtual User Owner { get;set; }
}

User entity

public class User {
  public virtual int ID { get;set; }
  public virtual string Name { get;set; }

  public virtual UserAddress Address {
    get {
      return address;
    }
    set {
      if (value != null) 
        value.Owner = this
      address = value
    }
  }
}

and the mapping for Adderss, UserAddress and user is like the following:

Address and its subclasses (Table per class hierarchy strategy):

<class name="Address" table="Address" abstract="true">
  <id name="ID">
    <generator class="identity"/>
  </id>
  <discriminator column="Ownertype" type="System.String" />
  <property name="addr1 />
  <property name="postcode" />
  ....
  <subclass name="UserAddress" discriminator-value="USER">
    <many-to-one name="Owner" column="Ownerid" />
  </subclass>
  <subclass name="OrgAddress" discriminator-value="ORG">
    <many-to-one name="Owner" column="Ownerid" />
  </subclass>
</class>

User:

<class name="User" table="User">
  <id name="ID">
    <generator class="identity"/>
  </id>
  <property name="Name" />
  ....
  <one-to-one name="Address" property-ref="Owner" cascade="all" />
</class>

is this the correct way to do this? do give me any other better alternative than this.

notes: i do not show here on Org entity and mapping, basically it has the same concept as user table. pardon me for any syntax error as I type this here, because the real entity name is not intuitive without docs to refer.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜