&引用;“多对一”;组件内问题NHibernate

&引用;“多对一”;组件内问题NHibernate,nhibernate,Nhibernate,通过以下映射,BillingAddress.Country映射正确,但ShippingAddress.Country始终为空。也许是因为相同的属性名称?但它们是不同的物体。。。知道怎么修吗? 谢谢 <?xml version="1.0" encoding="utf-8" ?> <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="KapsNet.QuickOffice.Models" namespace=

通过以下映射,BillingAddress.Country映射正确,但ShippingAddress.Country始终为空。也许是因为相同的属性名称?但它们是不同的物体。。。知道怎么修吗? 谢谢

<?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>


您应该从帐单和发货地址的映射中删除CountryID,因为它已在与国家/地区的多对一关系中定义。除此之外,映射看起来还不错。如果组件中的所有值都为null,那么组件将为null。因此,如果所有发货地址字段都为空,那么ShippingAddress组件将为空。

问题是,ShippingAddress实体只有国家字段为空,而CountryID=1。另一方面,BillingAddress也有CountryID=1,但Country字段绑定良好(不为空)。ShippingAddress的所有其他字段都绑定为OK。关于CountryID-由于后期绑定的原因,hbm生成器创建这些字段-当您只需要ID而不需要完整对象时,这对性能有好处。