tags:

views:

24

answers:

1

With the following mapping, BillingAddress.Country is mapped correctly but ShippingAddress.Country is always null. Maybe it's because of equal properties name? but they are different objects... Any idea how to fix it? Thanks ahead.

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="KapsNet.QuickOffice.Models" namespace="KapsNet.QuickOffice.Dto">
 <class name="Order" table="`Order`" >
  <id name="ID" type="System.Int32" column="ID">
   <generator class="identity"/>
  </id>
  <property name="CreateDate" column="CreateDate" type="System.DateTime" not-null="true" />
  <property name="ClientContactID" column="ClientContactID" type="System.Int32" not-null="true" />
  <component name="BillingAddress" class="AddressInfo">
   <property name="Address" column="BillingAddress" type="System.String" not-null="false" length="255"/>
   <property name="ZipCode" column="BillingZipCode" type="System.String" not-null="false" length="50"/>
   <property name="City" column="BillingCity" type="System.String" not-null="false" length="50"/>
   <property name="CountryID" column="BillingCountryID" type="System.Int32" not-null="true" />
   <property name="Phone" column="BillingPhone" type="System.String" not-null="false" length="50"/>
   <many-to-one name="Country" column="BillingCountryID" class="Country"  update="0"  insert="0" />
  </component>
  <component name="ShippingAddress" class="AddressInfo">
   <property name="Address" column="ShippingAddress" type="System.String" not-null="false" length="255"/>
   <property name="ZipCode" column="ShippingZipCode" type="System.String" not-null="false" length="50"/>
   <property name="City" column="ShippingCity" type="System.String" not-null="false" length="50"/>
   <property name="CountryID" column="ShippingCountryID" type="System.Int32" not-null="true" />
   <property name="Phone" column="ShippingPhone" type="System.String" not-null="false" length="50"/>
   <many-to-one name="Country" column="ShippingCountryID" class="Country"  update="0"  insert="0" />
  </component>
  <many-to-one name="ClientContact" column="ClientContactID" class="ClientContact"  update="0"  insert="0" />
  <bag name="OrderItemList" table="OrderItem" inverse="true" lazy="true" cascade="delete">
   <key column="OrderID" />
   <one-to-many class="OrderItem"/>
  </bag>
 </class>
</hibernate-mapping>
+1  A: 

You should remove CountryID from the mappings for billing and shipping address because it is already defined in the many-to-one relationship to Country. Other than that the mappings look okay. Components will be null if all of the values in the component are null. So, if all of the shipping address fields are null then the ShippingAddress component will be null.

Jamie Ide
The problem is that, only Country field is null for ShippingAddress entity, while CountryID = 1. In other side, BillingAddress also has CountryID = 1 but Country field is binded good (not null). All other fields of ShippingAddress are binded okey. About CountryID - the hbm generator creates these fields because of late binding reasons - when you need just ID but not full object, it is good for performance.
McWest