Reputation: 4809
I have Orders, Items and OrderItems entities with n:n relationship between Orders and Items table. I am using Fluent Hibernate for mapping, in "Order" entity there is "List" property which has "protective set". I am facing below problem, while inserting.
new Order.Items.Add(alreadyexistingitem);
Since its already existing item, in the orderitems table, a record needs to be inserted into orderitems table with new orderid and existing item id. But only new order details are inserted in order table not in associative table.
"Inverse" attribute is set for "items" bag in orders entity. So, I need to attach the newly added order while adding item like below but how can i do it as it is the list object with protective set.
Orders.Items.Add(alreadyexistingitem, *order = "neworder"*);
Below is the mapping:
model.Override<Order>(m =>
{
m.HasManyToMany(c => c.Items)
.Table(Constants.TABLE_PREFIX + "OrderItem")
.Inverse()
.Cascade.AllDeleteOrphan();
});
model.Override<Item>(m => m.HasManyToMany(c => c.Orders)
.Table(Constants.TABLE_PREFIX + "OrderItem")
.Cascade.None());
Upvotes: 0
Views: 345
Reputation: 71565
As a preliminary answer, NHibernate is smart enough to know when the same object reference is being used in two places. All you should need is a "HasManyToMany" relationship in the mapping between Order and Item.
Inverse should probably not be used on the orders side. Inverse tells NHibernate that the opposite side of the relationship has the power to define and break the relationship. Orders have items; items don't get to "choose" what order they belong to.
Upvotes: 2