所以我有两个表(产品和供应商)通过多对多关系连接。它们的编码如下:
class Product
{
public virtual uint id { get; set; }
public virtual string name { get; set; }
public virtual float stock { get; set; }
public virtual float value { get; set; }
public virtual Category category { get; set; }
public virtual ISet suppliers { get; set; }
public virtual ISet saleItems { get; set; }
public virtual ISet stockInlets { get; set; }
}
-
class Supplier
{
public virtual uint id { get; set; }
public virtual string name { get; set; }
public virtual string CNPJ { get; set; }
public virtual string addressLine1 { get; set; }
public virtual string addressLine2 { get; set; }
public virtual string telephone1 { get; set; }
public virtual string telephone2 { get; set; }
public virtual ISet products { get; set; }
public virtual ISet stockInlets { get; set; }
}
这就是我映射他们关系的方式:
<set name="suppliers" table="product_x_supplier" fetch="join" lazy="false">
<key column="product_id" />
<many-to-many class="Supplier" column="supplier_id" />
</set>
-
<set name="products" table="product_x_supplier" fetch="join" lazy="false">
<key column="supplier_id" />
<many-to-many class="Product" column="product_id" />
</set>
现在,我在插入新产品并尝试将相应的供应商链接到它时遇到问题。我确实设法更新了一个已经存在的,但没有创建一个新的。这是我尝试使用的(简化的)代码:
Product p = new Product()
{
name = "Test product",
stock = 0,
value = 2,
category = (Category)cats[0]
};
for (int i = 0; i < suppliers.Length; i++)
{
p.suppliers.Add(suppliers[i]);
}
repository.Add(p);
它告诉我Object reference not set to an instance of an object.
。我试图实例化该对象,做了类似的事情,p.suppliers = new IList<Supplier>();
但后来它抱怨了Cannot create an instance of the abstract class or interface 'System.Collections.Generic.IList<TCC.Hibernate.Domain.Classes.Supplier>'
。
任何帮助表示赞赏。