nhibernate <bag> exception - 非法访问加载集合</bag>

时间:2011-01-19 20:44:22

标签: nhibernate nhibernate-mapping

尝试使用NHibernate填充Supplier Domain中的“IList”属性时,我收到“非法访问加载集合”异常。我已经尝试了通过谷歌搜索获得的所有建议,但似乎没有任何帮助:(

以下是我的域对象和.HBM文件。非常感谢您的帮助/建议。

供应商域对象

namespace Inventory.DomainObjects
{
    [Serializable]
    public class Supplier
    {
        public virtual string SupplierID { get; set; }

        public virtual string Name { get; set; }
        public virtual string Description { get; set; }
        public virtual IList<Address> Address { get; set; }

    }
}

地址域对象

namespace Inventory.DomainObjects
{
    [Serializable]
    public class Address 
    {
        public virtual int AddressID { get; set; }
        public virtual string SupplierID { get; set; }

        public virtual string Line1 { get; set; }
        public virtual string Line2 { get; set; }
        public virtual string Line3 { get; set; }
    }
}

Supplier.HBM

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                   namespace="Inventory.DomainObjects"
                   assembly="Inventory">
  <class name="Supplier" table="Inv_Supplier">
    <id name="SupplierID" column="SupplierId" type="string"/>

    <property name="SupplierCode" column="Code" type="string"/>
    <property name="Name" column="SupplierName" type="string"/>
    <property name="Description" column="SupplierDescription" type="string"/>

    <bag name="Address" cascade="all" inverse="true" lazy="true">
      <key column="SupplierID" not-null="true"/>
      <one-to-many class="Address" not-found="ignore"/>
    </bag>

  </class>
</hibernate-mapping>

Address.HBM

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                   namespace="Inventory.DomainObjects"
                   assembly="Inventory">
  <class name="Address" table="Inv_Supplier_Address" lazy="false">
    <id name="AddressID" column="AddressId" type="integer"/>

    <property name="Line1" column="Line1" type="string"/>
    <property name="Line2" column="Line2" type="string"/>
    <property name="Line3" column="Line3" type="string"/>

    <many-to-one name="SupplierID" column="SupplierId" not-null="true" class="Supplier" />
  </class>
</hibernate-mapping>

1 个答案:

答案 0 :(得分:0)

这看起来很可疑:

<many-to-one name="SupplierID" column="SupplierId" 
       not-null="true" class="Supplier" />

您是否可以尝试删除上述行以查看问题是否消失?

如果这样可以解决问题,则应将many-to-one添加回来,如下所示:

namespace Inventory.DomainObjects
{
    [Serializable]
    public class Address 
    {
        public virtual int AddressID { get; set; }

        // CHANGED: reference supplier object instead of ID
        public virtual Supplier Supplier { get; set; } 

        public virtual string Line1 { get; set; }
        public virtual string Line2 { get; set; }
        public virtual string Line3 { get; set; }
    }
}

然后像这样更改你的hbm映射文件(引用Supplier属性而不是SupplierId

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                   namespace="Inventory.DomainObjects"
                   assembly="Inventory">
  <class name="Address" table="Inv_Supplier_Address" lazy="false">
    <id name="AddressID" column="AddressId" type="integer"/>

    <property name="Line1" column="Line1" type="string"/>
    <property name="Line2" column="Line2" type="string"/>
    <property name="Line3" column="Line3" type="string"/>

    <many-to-one name="Supplier" column="SupplierId" 
             not-null="true" class="Supplier" />
  </class>
</hibernate-mapping>