If you see the documentation for performance of the collections : http://nhibernate.info/doc/nhibernate-reference/performance.html#performance-collections-taxonomy
It says:
Bags are the worst case. Since a bag permits duplicate element values and has no index column, no primary key may be defined. NHibernate has no way of distinguishing between duplicate rows. NHibernate resolves this problem by completely removing (in a single DELETE) and recreating the collection whenever it changes. This might be very inefficient.
However I cannot confirm this case. For example if I have a simple parent child relation with cascade all, using bag, with the following code:
using (var sf = NHibernateHelper.SessionFactory)
using (var session = sf.OpenSession())
{
var trx = session.BeginTransaction();
var par = session.Query<Parent>().First();
var c = new Child { Id = 4, Name = "Child4" };
par.Children.Add(c);
trx.Commit();
}
I don't see any deletes, but an insert to child table and an update for parentid. This actually make sense. However it seems to contradict with the docs. What am I missing?
Id
s like yourChild
has (I assume it's a transient and mappedobject
in itself). I don't have time right now, but I suspect if you try it with aList<string>
mapped to a Bag, so you end up with a relation Table that has just two Columns, 'Parent_id' and 'StringValue' (with repeatedstrings
present) you will see the documentation described behavior. - starlight54Exception
upon adding a duplicate transient Entity on either side if it enforces Entity uniqueness in anyCollection
, I'm not certain on that either, my Nhib is a touch rusty at the moment :) - starlight54