重复集合角色映射 nHibernate - 当试图制作一个 - 多个映射时
Duplicate collection role mapping nHibernate - When trying to make one - many mapping
我正在学习 nHibernate,我正在尝试一对多映射。这是两个表产品和产品类型。
namespace NHibernateSample.Models
{
public class Product
{
public virtual Guid Id { get; set; }
public virtual string Name { get; set; }
public virtual string Category { get; set; }
public virtual bool Discontinued { get; set; }
public virtual IList<ProductType> ProductTypes { get; set; }
}
}
这是我的映射 XML
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="NHibernateSample"
namespace="NHibernateSample.Models">
<!-- more mapping info here -->
<class name="Product">
<id name="Id">
<generator class="guid" />
</id>
<property name="Name" />
<property name="Category" />
<property name="Discontinued" />
<bag name="ProductTypes">
<key column="ProductID" />
<one-to-many class="NHibernateSample.Models.ProductType,NHibernateSample" />
</bag>
</class>
</hibernate-mapping>
产品类型xml:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="NHibernateSample"
namespace="NHibernateSample.Models">
<!-- more mapping info here -->
<class name="ProductType">
<id name="ProductTypeID">
<generator class="increment"/>
</id>
<property name="ProductType1" column="ProductType"/>
<property name="ProductID" />
<many-to-one name="Product" class="Product">
<column name="ProductID" sql-type="int" not-null="true"/>
</many-to-one>
</class>
</hibernate-mapping>
映射在配置中
mapping assembly="NHibernateSample" />
当我尝试配置和构建会话工厂时
var cfg = new Configuration();
cfg.Configure();
m_SessionFactory = cfg.BuildSessionFactory();
我收到错误 "Association references unmapped class: NHibernateSample.Models.ProductType" 但我在 bin 文件夹中看到了 hbm 文件。
如果我显式添加程序集,
Assembly thisAssembly = typeof(Product).Assembly;
cfg.AddAssembly(thisAssembly);
我收到另一个错误 "Duplicate collection role mapping NHibernateSample.Models.Product.ProductTypes"
我做错了什么?这不是我应该映射外键的方式吗?
提前致谢
第一个问题似乎与 .hbm.xml 文件的错误设置有关。它总是必须有 (参见 MappingException: No persister for - NHibernate - Persisting an Adapter)
- xml 映射文件未被标记为 嵌入式资源
- xml 文件不是配置为映射源的 .dll 的一部分
<mapping assembly="MyProject.Data" />
(参见配置)
- xml 文件没有默认后缀 .hbm.xml
第二个问题(评论中)
Here I insert a product. I want to insert some ProductType to the
database like prod.ProductTypes = new
.... How can I do that
Product prod = new Product();
prod.Name = "Q3"; prod.Category = "Audi";
prod.Discontinued = false;
session.Save(prod);
...
解决方案是将集合的映射调整为使用级联:
<class name="Product">
<id name="Id">
<generator class="guid" />
</id>
<property name="Name" />
<property name="Category" />
<property name="Discontinued" />
<bag name="ProductTypes"
lazy="true" inverse="true" batch-size="25" cascade="all-delete-orphan"
>
<key column="ProductID" />
<one-to-many class="NHibernateSample.Models.ProductType,NHibernateSample" />
</bag>
</class>
(<bag>
上的所有这些设置是什么 - 检查 )
我会调整这个 POCO 定义
public class Product
{
public virtual Guid Id { get; set; }
public virtual string Name { get; set; }
public virtual string Category { get; set; }
public virtual bool Discontinued { get; set; }
//public virtual IList<ProductType> ProductTypes { get; set; }
IList<ProductType> _productTypes;
public virtual IList<ProductType> ProductTypes
{
get { return _productTypes ?? (_productTypes = new List<ProductType>()); }
set { _productTypes = value; }
}
}
(这只是为了确保列表是由 NHibernate 在加载时启动的,或者在其他情况下由我们启动的)
然后我们只需要分配两边
// product
Product prod = new Product();
prod.Name = "Q3"; prod.Category = "Audi";
prod.Discontinued = false;
// product type
ProductType productType = new ProudctType();
...
// both sides assigned
prod.ProductTypes.Add(productType);
productType.Product = prod;
session.Save(prod);
而且我们还应该将值类型的映射调整为只读属性
<property name="ProductID" insert="false" update="false" />
<many-to-one name="Product" class="Product">
<column name="ProductID" sql-type="int" not-null="true"/>
</many-to-one>
要了解更多详细信息,我不会错过:
我正在学习 nHibernate,我正在尝试一对多映射。这是两个表产品和产品类型。
namespace NHibernateSample.Models
{
public class Product
{
public virtual Guid Id { get; set; }
public virtual string Name { get; set; }
public virtual string Category { get; set; }
public virtual bool Discontinued { get; set; }
public virtual IList<ProductType> ProductTypes { get; set; }
}
}
这是我的映射 XML
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="NHibernateSample"
namespace="NHibernateSample.Models">
<!-- more mapping info here -->
<class name="Product">
<id name="Id">
<generator class="guid" />
</id>
<property name="Name" />
<property name="Category" />
<property name="Discontinued" />
<bag name="ProductTypes">
<key column="ProductID" />
<one-to-many class="NHibernateSample.Models.ProductType,NHibernateSample" />
</bag>
</class>
</hibernate-mapping>
产品类型xml:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="NHibernateSample"
namespace="NHibernateSample.Models">
<!-- more mapping info here -->
<class name="ProductType">
<id name="ProductTypeID">
<generator class="increment"/>
</id>
<property name="ProductType1" column="ProductType"/>
<property name="ProductID" />
<many-to-one name="Product" class="Product">
<column name="ProductID" sql-type="int" not-null="true"/>
</many-to-one>
</class>
</hibernate-mapping>
映射在配置中
mapping assembly="NHibernateSample" />
当我尝试配置和构建会话工厂时
var cfg = new Configuration();
cfg.Configure();
m_SessionFactory = cfg.BuildSessionFactory();
我收到错误 "Association references unmapped class: NHibernateSample.Models.ProductType" 但我在 bin 文件夹中看到了 hbm 文件。
如果我显式添加程序集,
Assembly thisAssembly = typeof(Product).Assembly;
cfg.AddAssembly(thisAssembly);
我收到另一个错误 "Duplicate collection role mapping NHibernateSample.Models.Product.ProductTypes"
我做错了什么?这不是我应该映射外键的方式吗? 提前致谢
第一个问题似乎与 .hbm.xml 文件的错误设置有关。它总是必须有 (参见 MappingException: No persister for - NHibernate - Persisting an Adapter)
- xml 映射文件未被标记为 嵌入式资源
- xml 文件不是配置为映射源的 .dll 的一部分
<mapping assembly="MyProject.Data" />
(参见配置) - xml 文件没有默认后缀 .hbm.xml
第二个问题(评论中)
Here I insert a product. I want to insert some ProductType to the database like
prod.ProductTypes = new
.... How can I do thatProduct prod = new Product(); prod.Name = "Q3"; prod.Category = "Audi"; prod.Discontinued = false; session.Save(prod); ...
解决方案是将集合的映射调整为使用级联:
<class name="Product">
<id name="Id">
<generator class="guid" />
</id>
<property name="Name" />
<property name="Category" />
<property name="Discontinued" />
<bag name="ProductTypes"
lazy="true" inverse="true" batch-size="25" cascade="all-delete-orphan"
>
<key column="ProductID" />
<one-to-many class="NHibernateSample.Models.ProductType,NHibernateSample" />
</bag>
</class>
(<bag>
上的所有这些设置是什么 - 检查
我会调整这个 POCO 定义
public class Product
{
public virtual Guid Id { get; set; }
public virtual string Name { get; set; }
public virtual string Category { get; set; }
public virtual bool Discontinued { get; set; }
//public virtual IList<ProductType> ProductTypes { get; set; }
IList<ProductType> _productTypes;
public virtual IList<ProductType> ProductTypes
{
get { return _productTypes ?? (_productTypes = new List<ProductType>()); }
set { _productTypes = value; }
}
}
(这只是为了确保列表是由 NHibernate 在加载时启动的,或者在其他情况下由我们启动的)
然后我们只需要分配两边
// product
Product prod = new Product();
prod.Name = "Q3"; prod.Category = "Audi";
prod.Discontinued = false;
// product type
ProductType productType = new ProudctType();
...
// both sides assigned
prod.ProductTypes.Add(productType);
productType.Product = prod;
session.Save(prod);
而且我们还应该将值类型的映射调整为只读属性
<property name="ProductID" insert="false" update="false" />
<many-to-one name="Product" class="Product">
<column name="ProductID" sql-type="int" not-null="true"/>
</many-to-one>
要了解更多详细信息,我不会错过: