首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >nhibernate <bag>异常-非法访问加载集合

nhibernate <bag>异常-非法访问加载集合
EN

Stack Overflow用户
提问于 2011-01-20 04:44:22
回答 1查看 4.6K关注 0票数 1

在尝试使用NHibernate填充供应商域中的"IList“属性时,我遇到了”非法访问加载集合“异常。我尝试了通过谷歌搜索得到的所有建议,但似乎没有任何帮助:

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

供应商域对象

代码语言:javascript
复制
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; }

    }
}

地址域对象

代码语言:javascript
复制
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

代码语言:javascript
复制
<?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

代码语言:javascript
复制
<?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>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-01-20 04:55:19

这看起来很可疑:

代码语言:javascript
复制
<many-to-one name="SupplierID" column="SupplierId" 
       not-null="true" class="Supplier" />

您可以尝试删除上面的行,看看问题是否消失了吗?

如果此操作解决了问题,则应按如下方式重新添加many-to-one

代码语言:javascript
复制
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

代码语言:javascript
复制
<?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>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4740503

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档