NHibernate:使用Fluent Nhibernate保存子对象 [英] NHibernate: Using Fluent Nhibernate to save child objects
问题描述
在我的对象中创建一个新的ShoppingCart对象。
$ b
ShoppingCart cart = CreateOrGetCart();
然后我从数据库中添加一个现有的产品到开始。
cart.AddItem(product);
这只是一个将项目添加到IList的简单包装。 b
public virtual void AddItem(Product product)
{
Items.Add(new ShoppingCartItem {Quantity = 1,Product = product}) ;
}
然后在Repository上调用SaveOrUpdate
Repository.SaveOrUpdate(cart);
看起来像这样:
Session.SaveOrUpdate(entity);
返回实体;
$ b我使用Fluent NHibernate进行映射:
public ShoppingCartItemMap()
{
WithTable(ShoppingCartItems);
Id(x => x.ID,ShoppingCartItemId);
Map(x => x.Quantity);
参考(x => x.Cart,ShoppingCartId)。Cascade.SaveUpdate();
参考文献(x => x.Product,ProductId);
$ b public ShoppingCartMap()
{
WithTable(ShoppingCarts);
Id(x => x.ID,ShoppingCartId);
Map(x => x.Created);
Map(x => x.Username);
HasMany< ShoppingCartItem>(x => x.Items)
.IsInverse()。Cascade.SaveUpdate()
.WithKeyColumn(ShoppingCartId)
.AsBag();数据库模式(SQL Server 2005)也相当通用:
$ b $ pre $ code CREATE TABLE [dbo]。[ShoppingCarts]
(
[ShoppingCartID] [int] NOT NULL IDENTITY(1, 1),
[用户名] [nvarchar](50)NOT NULL,
[创建] [日期时间] NOT NULL
)
GO
ALTER TABLE [dbo] 。[ShoppingCarts] ADD CONSTRAINT [PK_ShoppingCarts] PRIMARY KEY CLUSTERED([ShoppingCartID])
GO
CREATE TABLE [dbo]。[ShoppingCartItems]
(
[ShoppingCartItemId] [int]不是NULL IDENTITY(1,1),
[ShoppingCartId] [int]不是NULL,
[ProductId] [int] NOT NULL,
[Quantity] [int] NOT NULL
)
GO
ALTER TABLE [dbo]。[ShoppingCartItems] ADD CONSTRAINT [PK_ShoppingCartItems] PRIMARY KEY CLUSTERED([ShoppingCartItemId])
GO
ALTER TABLE [dbo]。[ShoppingCartItems] ADD CONSTRAINT [FK_ShoppingCartItems_Products] FOREIGN KEY([ProductId])REFERENCES [dbo]。[Products]([ProductId])
GO
ALTER TABLE [ [ShoppingCartId]] [ShoppingCartItems] ADD CONSTRAINT [FK_ShoppingCartItems_ShoppingCarts] FOREIGN KEY([ShoppingCartId])REFERENCES [dbo]。[ShoppingCarts]([ShoppingCartID])
GO
当我SaveOrUpdate我的ShoppingCart时,为什么没有任何ShoppingCartItems也被保存?
请帮忙。
谢谢
更新:
在交易中包装它提供了更多信息:
lockquote
无法将值NULL插入'ShoppingCartId'列,表
'WroxPizza.dbo.ShoppingCartItems';列不允许空值。
INSERT失败。该语句已被终止。
这是因为它是一个新的购物车。
我对流利的nhibernate有这个确切的问题。映射一对多关系似乎存在一个小错误,并且关系不会自动保存或级联。 请在这里查看关于它在Fluent NHibernate组上发表的帖子 。
显然,有人正在处理这个问题!
我可以肯定地说这个是您的代码的问题,但它看起来非常相似,我有一个问题。希望有所帮助!
In my system, I have two entities - ShoppingCart and ShoppingCartItem. Fairly generic use-case. However, when I save my ShoppingCart, none of the items are being saved to the DB.
Within my object, I create a new ShoppingCart object.
ShoppingCart cart = CreateOrGetCart();
I then add an existing Product which I got from the database to the start.
cart.AddItem(product);
This is just a simple wrapper to add the item to the IList.
public virtual void AddItem(Product product)
{
Items.Add(new ShoppingCartItem { Quantity = 1, Product = product });
}
I then call SaveOrUpdate on the Repository
Repository.SaveOrUpdate(cart);
Which looks like this:
public T SaveOrUpdate(T entity)
{
Session.SaveOrUpdate(entity);
return entity;
}
I'm using Fluent NHibernate for the mapping:
public ShoppingCartItemMap()
{
WithTable("ShoppingCartItems");
Id(x => x.ID, "ShoppingCartItemId");
Map(x => x.Quantity);
References(x => x.Cart, "ShoppingCartId").Cascade.SaveUpdate();
References(x => x.Product, "ProductId");
}
public ShoppingCartMap()
{
WithTable("ShoppingCarts");
Id(x => x.ID, "ShoppingCartId");
Map(x => x.Created);
Map(x => x.Username);
HasMany<ShoppingCartItem>(x => x.Items)
.IsInverse().Cascade.SaveUpdate()
.WithKeyColumn("ShoppingCartId")
.AsBag();
}
Database Schema (SQL Server 2005) is also fairly generic:
CREATE TABLE [dbo].[ShoppingCarts]
(
[ShoppingCartID] [int] NOT NULL IDENTITY(1, 1),
[Username] [nvarchar] (50) NOT NULL,
[Created] [datetime] NOT NULL
)
GO
ALTER TABLE [dbo].[ShoppingCarts] ADD CONSTRAINT [PK_ShoppingCarts] PRIMARY KEY CLUSTERED ([ShoppingCartID])
GO
CREATE TABLE [dbo].[ShoppingCartItems]
(
[ShoppingCartItemId] [int] NOT NULL IDENTITY(1, 1),
[ShoppingCartId] [int] NOT NULL,
[ProductId] [int] NOT NULL,
[Quantity] [int] NOT NULL
)
GO
ALTER TABLE [dbo].[ShoppingCartItems] ADD CONSTRAINT [PK_ShoppingCartItems] PRIMARY KEY CLUSTERED ([ShoppingCartItemId])
GO
ALTER TABLE [dbo].[ShoppingCartItems] ADD CONSTRAINT [FK_ShoppingCartItems_Products] FOREIGN KEY ([ProductId]) REFERENCES [dbo].[Products] ([ProductId])
GO
ALTER TABLE [dbo].[ShoppingCartItems] ADD CONSTRAINT [FK_ShoppingCartItems_ShoppingCarts] FOREIGN KEY ([ShoppingCartId]) REFERENCES [dbo].[ShoppingCarts] ([ShoppingCartID])
GO
When I SaveOrUpdate my ShoppingCart, why isn't any ShoppingCartItems also being saved?
Please help.
Thanks
Ben
UPDATE: Wrapping it in a transaction providng me with some more info:
Cannot insert the value NULL into column 'ShoppingCartId', table 'WroxPizza.dbo.ShoppingCartItems'; column does not allow nulls. INSERT fails. The statement has been terminated.
This is because it's a new cart.
I had this exact problem with fluent nhibernate. There appears to be a small bug in mapping one-to-many relationships and the relationships not automatically saving or cascading. See here for a posting about it on the Fluent NHibernate group.
Apparently, there is someone working on the issue!
I can;t say for sure that this is the issue with your code but it looks very similar to a problem I had. Hope that helps some!
这篇关于NHibernate:使用Fluent Nhibernate保存子对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!