i'm trying batch insert , it's not working. thought had working seems have broken , i'd appreciate if show me what.
edit - here's database schema:
create table [dbo].[categories]( [id] [int] identity(1,1) not null, [name] [nvarchar](100) not null, constraint [pk_categories] primary key clustered ([id]) ) create table [dbo].[producttopsellerscategory]( [productid] [int] not null, [categoryid] [int] not null, [order] [int] not null, constraint [pk_producttopsellerscategory] primary key clustered ([productid], [categoryid]) ) alter table [dbo].[producttopsellerscategory] add constraint [fk_producttopsellerscategory_products] foreign key ([productid]) references [dbo].[products] ([id]), constraint [fk_producttopsellerscategory_categories] foreign key ([categoryid]) references [dbo].[categories] ([id])
i have following entities:
public class category { public virtual int id { get; set; } public virtual string name { get; set; } } public class producttopsellercategory { public virtual producttopsellercategoryidentifier id { get; set; } private product _product; public virtual product product { { return _product; } set { _product = value; id.productid = _product.id; } } private category _category; public virtual category category { { return _category; } set { _category = value; id.categoryid = _category.id; } } [required] public virtual int order { get; set; } public producttopsellercategory() { id = new producttopsellercategoryidentifier(); } } [serializable] public class producttopsellercategoryidentifier { public virtual int productid { get; set; } public virtual int categoryid { get; set; } #region composite id members public override bool equals(object obj) { if (obj == null || !(obj producttopsellercategoryidentifier)) return false; var = (producttopsellercategoryidentifier)obj; return productid == i.productid && categoryid == i.categoryid; } public override int gethashcode() { return tostring().gethashcode(); } public override string tostring() { return productid + "|" + categoryid; } #endregion }
with corresponding fluent mappings:
public class categorymap : classmap<category> { public categorymap() { table("categories"); id(x => x.id); map(x => x.name); } } public class producttopsellercategorymap : classmap<producttopsellercategory> { public producttopsellercategorymap() { table("producttopsellerscategory"); compositeid(x => x.id) .keyproperty(x => x.productid) .keyproperty(x => x.categoryid); references(x => x.product).readonly(); references(x => x.category).readonly(); map(x => x.order, "[order]"); } }
now when say:
var category = new category() { name = "test 1" }; var product = session.get<product>(1); var topseller = new producttopsellercategory() { product = product, category = category }; session.saveorupdate(category); session.saveorupdate(topseller); session.transaction.commit();
it throws error:
the insert statement conflicted foreign key constraint "fk_producttopsellerscategory_categories". conflict occurred in database "xxx", table "dbo.categories", column 'id'. statement has been terminated.
i've tried simplify example as possible. i'd appreciate help. thanks
you have one-to-many relationship between category , producttopsellercategory many side mapped. use inverse attribute on collection mapped on 1 side don't have mapped suggest:
using (var txn = session.begintransaction()) { var category = new category() { name = "test 1" }; session.save(category); session.flush(); var product = session.get<product>(1); var producttopsellercategory = new producttopsellercategory() { product = product, category = category }; session.save(producttopsellercategory); txn.commit(); }
the problem original code nhibernate attempting insert new producttopsellercategory update category. it's doing because inverse attribute not set. forcing nhibernate insert new category flushing session should resolve problem.
Comments
Post a Comment