开发者

"many-to-one" within component problem NHibernate

开发者 https://www.devze.com 2023-01-07 17:17 出处:网络
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 i

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-ma开发者_高级运维pping 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>


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.

0

精彩评论

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