nhibernate <bag>исключение - незаконный доступ к загрузке коллекции - PullRequest
1 голос
/ 19 января 2011

Я получаю исключение "незаконный доступ к загрузке коллекции" при попытке заполнить свойство "IList" в Домене поставщика с помощью NHibernate. Я перепробовал все предложения, которые я нашел, но ничего не помогло: (

Вот мои доменные объекты и файлы .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 голосов
/ 19 января 2011

Это выглядит подозрительно:

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