NHibernate IStatelessSession 和多对多关系
我在两个实体之间存在多对多关系。作为批处理过程的一部分,我正在创建大量这些实体并将它们关联在一起。这是使用 IStatelessSession
。
我正在使用 NHibernate 3.0。
实体:
class Entity1
{
ICollection<Entity2> Entities { get; set; }
}
class Entity2
{
ICollection<Entity1> Entities { get; set; }
}
基本上批处理代码看起来像这样:
var entity1 = new Entity1();
var entity2 = new Entity2();
entity1.Entities.Add(entity2);
entity2.Entities.Add(entity1);
Session.Insert(entity1); // IStatelessSession.Insert
Session.Insert(entity2);
两个实体被正确持久化,但是它们之间的关系表没有随着两个实体之间的关系而更新。
我知道这与无状态会话不跟踪对象这一事实有关。但我该如何实现多对多的持久性呢?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
无状态会话会忽略集合。您应该使用常规 ISession 并以合理的时间间隔(例如每 500 个对象)调用
ISession.Clear
。这样,一级缓存就不会变得臃肿,并且您将获得不错的性能。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.