Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp
Hibernate version:
2.0.1.4
Mapping documents:
<class name="FlexiCommerce.Components.PersistenceItem,FlexiCommerce" table="FlexiItem">
<cache usage="read-write" />
<id name="ID" column="ID" type="Int32" unsaved-value="0" access="property">
<generator class="native" />
</id>
<discriminator column="Type" type="String" />
<property name="Created" access="property" not-null="true" />
<property name="Published" access="property" />
<property name="Updated" access="property" not-null="true" />
<property name="Expires" access="property" />
<property name="Name" access="property" length="255" />
<property name="SortOrder" access="property" not-null="true" />
<property name="Visible" access="property" not-null="true" />
<property name="SavedBy" access="property" length="50" />
</class>
<subclass name="FlexiCommerce.Components.Category,FlexiCommerce" extends="FlexiCommerce.Components.PersistenceItem,FlexiCommerce" discriminator-value="Category" lazy="false">
<bag name="Products" lazy="true" table="Flexi_ProductCategoryMapping">
<key column="ID"></key>
<many-to-many class="FlexiCommerce.Components.Product,FlexiCommerce"></many-to-many>
</bag>
</subclass>
<subclass name="FlexiCommerce.Components.Product,FlexiCommerce" extends="FlexiCommerce.Components.PersistenceItem,FlexiCommerce" discriminator-value="Product" lazy="false">
<bag name="Categories" lazy="true" table="Flexi_ProductCategoryMapping">
<key column="ID"></key>
<many-to-many class="FlexiCommerce.Components.Category,FlexiCommerce"></many-to-many>
</bag>
</subclass>
Code between sessionFactory.openSession() and session.close():
using (repository)
{
category1 = new Category();
category1.Name = "Category 1";
repository.Save(category1);
repository.Flush();
}
using (repository)
{
category2 = new Category();
category2.Name = "Category 2";
repository.Save(category2);
repository.Flush();
}
using(repository)
{
product1 = new Product();
product1.Name = "Product 1";
product1.Categories.Add(category1);
engine.Persister.Save(product1);
engine.Persister.Flush();
}
using (repository)
{
product2 = new Product();
product2.Categories.Add(category1);
product2.Categories.Add(category2);
product2.Name = "Product 2";
engine.Persister.Save(product2);
engine.Persister.Flush();
}
Full stack trace of any exception that occurs:
No exception, just lack of knowlege ;)
Name and version of the database you are using:
I am using SQLServer 2005
My failed unit tests
[Test]
public void ProductsHaveCategories()
{
using (repository)
{
Product product = repository.Get(product1.ID) as Product;
Assert.AreEqual(1,product.Categories.Count);
EnumerableAssert.Contains(product.Categories, category1);
product = repository.Get(product2.ID) as Product;
Assert.AreEqual(2, product.Categories.Count);
EnumerableAssert.Contains(product.Categories, category1);
EnumerableAssert.Contains(product.Categories, category2);
}
}
[Test]
public void CategoriesHaveProducts()
{
using (repository)
{
Category category= repository.Get(category1.ID) as Category;
Assert.AreEqual(2, category.Products.Count);
EnumerableAssert.Contains(category.Products, product1);
category= repository.Get(category2.ID) as Category;
Assert.AreEqual(1, category.Products.Count);
EnumerableAssert.Contains(category.Products, product1);
EnumerableAssert.Contains(category.Products, product2);
}
}
TestCase 'FlexiCommerce.Tests.Persistence.NH.ProductRepositoryTests.CategoriesHaveProducts'
failed:
Expected: 2
But was: 0
C:\Users\Paul\Documents\Visual Studio 2008\Projects\FlexiCommerce\FlexiCommerce\FlexiCommerce.Tests\Persistence\NH\ProductRepositoryTests.cs(112,0): at FlexiCommerce.Tests.Persistence.NH.ProductRepositoryTests.CategoriesHaveProducts()
The test ProductsHaveCategories passes. The many-to-many association doesn't work the other way (CategoriesHaveProducts). I tried inserting inverse=true to both subclasses's bag but then both tests fail and the association doesn't doesnt even show in the database.
Im sure the problem is from my lack of knowledge using the bag element in many-to-many relationships.
If it is worth mentioning, each subclass is an extension of one class (one table).