I am learning nHibernate and I was trying one to many mapping. Here are the two tables Product and Product Type.
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; }
}
}
Here are my mapping XMLs
<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>
Product type 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>
The mappings are in the config
mapping assembly="NHibernateSample" />
When I try configuring and build session factory
var cfg = new Configuration();
cfg.Configure();
m_SessionFactory = cfg.BuildSessionFactory();
I get the error saying "Association references unmapped class: NHibernateSample.Models.ProductType" but I see the hbm file in the bin folder.
If I add the assembly explicitly,
Assembly thisAssembly = typeof(Product).Assembly;
cfg.AddAssembly(thisAssembly);
I get another error saying "Duplicate collection role mapping NHibernateSample.Models.Product.ProductTypes"
What am I doing wrong ? Is it not the way I should map the foreign keys ? Thanks in advance