【问题标题】:nhibernate <bag> exception - illegal access to loading collectionnhibernate <bag> 异常 - 非法访问加载集合
【发布时间】:2011-01-20 04:55:18
【问题描述】:

我在尝试使用 NHibernate 填充供应商域中的“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; }
    }
}

供应商.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>

地址.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>

【问题讨论】:

    标签: nhibernate nhibernate-mapping


    【解决方案1】:

    这看起来很可疑:

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

    【讨论】:

    • 非常感谢安迪。你的建议奏效了。愚蠢的我,我没有注意到:( 是否可以从另一个表 (Inv_Images) 填充供应商对象 (IList Images) 中的列表属性,该表不包含主键但包含供应商的多个图像,供应商 ID 为外键?我无法为包含 NO 主键的图像表定义 HBM 映射文件。
    猜你喜欢
    • 1970-01-01
    • 2011-05-18
    • 2011-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多