NHibernate IStatelessSession and Many-to-Many Relationships
I have a many-to-many relationship between two entities. As part of a batch process, I am creating a large number of these entities and relating them together. This is using an IStatelessSession
.
I am using NHibernate 3.0.
Entities:
class Entity1
{
ICollection<Entity2> Entities { get; set; }
}
class Entity2
{
ICollection<Entity1> Entities { get; set; }
}
Basically the batch code looks something like:
var entity1 = new Entity1();
var entity2 = new Entity2();
entity1.Entities.Add(entity2);
entity2.Entities.Add(entity1);
Session.Insert(entity1); // IStatelessSession.Insert
Session.Insert(entity2);
The two entities are correctly persisted, however the relationship table between them is not updated with the relationship between the two entities.
I understand that this has t开发者_开发问答o do with the fact that stateless sessions don't track the objects. But how would I go about achieving many-to-many persistence?
Collections are ignored by stateless sessions. You should use regular ISession and call ISession.Clear
at a reasonable interval (say every 500 objects). This way 1st level cache will not get bloated and you will have decent performance.
精彩评论