2011-01-19 44 views
1

我尝试使用NHibernate填充供应商域中的“IList”属性时,出现“非法访问加载集合”异常。我已经尝试了所有的建议我通过在google但似乎没有任何帮助:(nhibernate <bag>异常 - 非法访问加载收集

这里是我的域对象和.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> 

回答

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

谢谢了Andy,你的建议奏效了,傻了,我没有注意到:(是否有可能从另一个包含NO PRIMAR的表格(Inv_Images)填充供应商对象中的列表属性(IList 图片) Y KEY,但包含SupplierId作为外键的供应商的多个图像?我无法为包含无主键的图像表定义HBM映射文件。 – Alex 2011-01-19 22:13:32