3

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 to do with the fact that stateless sessions don't track the objects. But how would I go about achieving many-to-many persistence?

codekaizen
  • 26,990
  • 7
  • 84
  • 140
Salty
  • 100
  • 1
  • 7

1 Answers1

1

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.

Dmitry
  • 17,078
  • 2
  • 44
  • 70