I keep struggling when mapping my collections using NHibernate. Please see this simple example and call stack. Is this a bug in NHibernate?
Hibernate version:
1.2.1
Mapping documents:
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="Recipe.Recipe, Recipe" table="Recipes" lazy="false" polymorphism="implicit">
<id name="RecipeName" column="RecipeName" type="String">
<generator class="assigned" />
</id>
<component name="Ingredients">
<array name="Ingredient" table="Ingredients">
<key column="RecipeNameFK"/>
<index column="IngredientIndex"/>
<composite-element class="Recipe.Ingredient, Recipe">
<property name="IngredientName" column="IngredientName" type="String" />
</composite-element>
</array>
</component>
</class>
<class name="Recipe.Ingredient, Recipe" table="Ingredients" lazy="false" polymorphism="implicit">
<id name="IngredientName" column="IngredientName" type="String">
<generator class="assigned" />
</id>
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():Code:
using (ITransaction transaction = _session.BeginTransaction())
{
Recipe recipe = new Recipe();
recipe.RecipeName = "Pesto";
recipe.Ingredients.Ingredient = new Ingredient[] {
new Ingredient("Basil"),
new Ingredient("Pine Nuts"),
new Ingredient("Olive Oil"),
new Ingredient("Cheese")
};
_session.Save(recipe);
transaction.Commit();
}
Full stack trace of any exception that occurs:Quote:
Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object.
at NHibernate.Impl.SessionImpl.UpdateReachableCollection(IPersistentCollection coll, IType type, Object owner)
at NHibernate.Impl.FlushVisitor.ProcessCollection(Object collection, CollectionType type)
at NHibernate.Impl.AbstractVisitor.ProcessValue(Object value, IType type)
at NHibernate.Impl.AbstractVisitor.ProcessValues(Object[] values, IType[] types)
at NHibernate.Impl.AbstractVisitor.ProcessComponent(Object component, IAbstractComponentType componentType)
at NHibernate.Impl.AbstractVisitor.ProcessValue(Object value, IType type)
at NHibernate.Impl.AbstractVisitor.ProcessValues(Object[] values, IType[] types)
at NHibernate.Impl.SessionImpl.FlushEntity(Object obj, EntityEntry entry)
at NHibernate.Impl.SessionImpl.FlushEntities()
at NHibernate.Impl.SessionImpl.FlushEverything()
at NHibernate.Impl.SessionImpl.Flush()
at NHibernate.Transaction.AdoTransaction.Commit()
at Recipe.HibernateDriver.StoreSomething() in Q:\Report\DefinitionDatabase\NHibernate\Recipe\HibernateDriver.cs:line 65
at Recipe.Program.Main(String[] args) in Q:\Report\DefinitionDatabase\NHibernate\Recipe\Program.cs:line 14
Name and version of the database you are using:SQLite v3
The generated SQL (show_sql=true):Code:
CREATE TABLE Recipes (
RecipeName text PRIMARY KEY NOT NULL
);
CREATE TABLE Ingredients (
IngredientName text PRIMARY KEY NOT NULL,
RecipeNameFK text,
IngredientIndex integer
);