【发布时间】:2009-07-20 01:30:19
【问题描述】:
我有一个地址类,我想将它用于其他对象的地址信息。下面我粘贴 2 个使用 Address 类的示例类 Company 和 Person
public class Company
{
public virtual string Name { get; set; }
public virtual string Phone { get; set; }
public virtual string Fax { get; set; }
public virtual Address Address { get; set; }
}
public class Person
{
public virtual string Name { get; set; }
public virtual string Phone { get; set; }
public virtual string Fax { get; set; }
public virtual Address Address { get; set; }
}
public class Address
{
public virtual string Address1 { get; set; }
public virtual string CityName { get; set; }
public virtual string StateName { get; set; }
public virtual string CountryName { get; set; }
public virtual AddressOf AddressOf { get; set; }
public virtual object Owner { get; set; }
}
public enum AddressOf : byte
{
Company,
Person
}
我的计划是有一个地址表,我将保存公司客户的地址等。 每个地址都有其唯一的 ID,并将通过另一个 owner_id 和 owner_type 列与地址的所有者相关联。
我通常的做法是在所有需要地址信息的表上都有地址列(然后我可以为 nhibernate 进行组件映射)但现在我认为这可能没有必要,我可以在一个上收集所有地址公用表..
现在我面临的挑战是如何将这些映射到 nhibernate。我想到了一对一的映射,但不知道如何映射 owner_id 列,该列应该是从所有者/公司、个人等外部生成的。
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" >
<class name="Company" table="Companiess">
<id name="ID" column="ID" type="Int32" unsaved-value="0">
<generator class="native" />
</id>
<property name="Name" />
<property name="Phone" />
<property name="Fax" />
<one-to-one name="Address" class="Address" />
</class>
</hibernate-mapping>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="Addresses" table="Addresses ">
<id name="ID" column="ID" type="Int32" unsaved-value="0">
<generator class="native" />
</id>
<property name="Address1" />
<property name="CityName" />
<property name="StateName" />
<property name="CountryName" />
<one-to-one "here is where im having trouble, I need to map owners id to owner_id column of Addresses table" ></one-to-one>
</class>
</hibernate-mapping>
如果您发现架构问题,也请告诉我,因为我以前没有以这种方式使用过单独的地址表,而且我对此还不是很满意。 谢谢!
【问题讨论】:
标签: c# nhibernate nhibernate-mapping