开发者

NHibernate Mapping for class contained by multiple classes

I have a p开发者_如何学Croblem in NHibernate mapping. I have Class Company, Person and Address; Company and Person both can have Addresses hence i have taken Address in both. To store this I have tables Company, Person and Address. Now Company will have Address object and Person will also have Address Object so Address should also have reference to Company and Person object. So I created two child classes of Address 1. CompanyAddress 2. PersonAddress and in Database I created two more tables Company_Address and Person_Address. Now In Address.hbm.xml i have added Joined subclass for both CompanyAddress and PersonAddress which are referring to Company_Address and Person_Address tables respectively.

Now CompanyAddress class is having company object in it and PersonAddress class is having Person object in it.

Company_Address is having 2 columns AddressId(PK) and CompanyId(FK)->Company Person_Address is having 2 columns AddressId(PK) and PersonId(FK)->Person

I have created one-to-one mapping in Company.hbm.xml for Address. When i Save Company object every table is populating properly except Company_Address. AddressId is getting stored but CompanyId is not getting stored.

I have no idea how to get this working

If someone can faced this problem please help.

Thanks in advance!!! Pawan Shukla


It sounds like you may have over-normalized here, given that you have set up one-to-one mappings. What might be easier (and WAY cleaner in code) is to place your address fields in the Company and Person tables themselves, then set up a simple address object and treat it as a component. Here's my address class:

public class StreetAddress
{
    public string CountryCode { get; set; }
    public string Street { get; set; }
    public string City { get; set; }
    public string County { get; set; }
    public string StateCode { get; set; }
    public string PostalCode { get; set; }

    public StreetAddress()
    {
        // Constructor for NHibernate
    }

    public StreetAddress(string countryCode, string street, string city, string county, string stateCode, string postalCode)
    {
        CountryCode = countryCode;
        Street = street;
        City = city;
        County = county;
        StateCode = stateCode;
        PostalCode = postalCode;
    }
}

Then you treat the address as a component, and map it like this:

<component name="Address" insert="true" update="true" optimistic-lock="true">
  <property name="CountryCode">
    <column name="Address_CountryCode" />
  </property>
  <property name="Street">
    <column name="Address_Street" />
  </property>
  <property name="City">
    <column name="Address_City" />
  </property>
  <property name="County">
    <column name="Address_County" />
  </property>
  <property name="StateCode">
    <column name="Address_StateCode" />
  </property>
  <property name="PostalCode">
    <column name="Address_PostalCode" />
  </property>
</component>


Don't do it this way. Address is clearly not an entity (and hence has no table of it's own and no primary key). What I would rather do is to model it as a component. A sample mapping might look like this:

<class name="Company"
    table="Company">

    <id name="Id">
        <generator class="identity"/>
    </id>
    <property name="CompanyName" />
    <component name="Address">
        <property name="Street"/>
        <property name="HouseNumber"/>
        <property name="City"/>
        <property name="PostOffice"/>
    </component>
</class>

Just google a bit. In DDD there's a notion of a value object opposed to an entity, and the way to model value objects in NHibernate is to use components.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜