NHibernate 3.0 beta1 Bidirectional One-To-Many Cannot Add Child Object
In short, the problem is that, when add child object to the collection property of the parent object without explicit setting the parent property of the child object, the insert will fail. Let's take a example:
NOTE: I'm using NHibernate 3.0 beta1.
Example: Product-Category Senario:
(1) Database Schema:
- Category (Id, Name)
- Product (Id, Name, Price, CategoryId)
(2) C# Code for Domain Models
public class Category
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
public virtual IList<Product> Products { get; private set; }
}
public class Product
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
public virtual decimal Price { get; set; }
public virtual Category Category { get; set; }
}
(3) Mappings
<class name="Category" table="Category">
<id name="Id" column="Id">
<generator class="identity" />
</id>
<property name="Name" />
<bag name="Products" inverse="true" cascade="all">
<key column="CategoryId" />
<one-to-many class="Core.Produc开发者_JAVA技巧t"/>
</bag>
</class>
<class name="Product" table="Product">
<id name="Id" column="Id">
<generator class="identity" />
</id>
<property name="Name" />
<property name="Price" />
<many-to-one name="Category" column="CategoryId" />
</class>
(4) Calling Code
using (var session = sessionFactory.OpenSession())
{
Category category = session.Query<Category>().FirstOrDefault();
Product product = new Product
{
Name = "test",
Price = 50
};
category.Products.Add(product);
// Here, the p.Category is null, but it should NOT be null
// And if now I commit the changes the the database,
// And exception will be thrown: Cann't insert null to column CategoryId
}
When the category.Products.Add(product)
is executed, the product.Category
shoule be object category
! If I explicit set the product.Category
to category, the commit operation will succeed.
Why this? The bug of NHibernate 3.0 beta1 or others?
This behaves exactly as documented.
6.4. One-To-Many Associations
NHibernate will NOT set product.Category
for you. The usual way to avoid forgetting this is adding an AddProduct
method to category which adds the product to the Products collection and sets the Category property.
精彩评论