使用 Criteria API 在 NHibernate 中急切地加载集合
我有一个实体 A,它有许多实体 B 和实体 C。所有实体 A、B 和 C 都有一些引用 x、y 和 z,应该立即加载。
我想从数据库中读取所有实体 A,并使用标准 API 急切地加载 B 和 C 的集合。 到目前为止,我已经能够急切地获取“A”中的参考文献了。但是,当加载集合时,其中的引用会被延迟加载。
下面是我的做法
AllEntities_A =
_session.CreateCriteria(typeof(A))
.SetFetchMode("x", FetchMode.Eager)
.SetFetchMode("y", FetchMode.Eager)
.List<A>().AsQueryable();
使用 Fluent 进行实体 A 的映射如下所示。 _B 和 _C 是 B 和 _C 的私有 IList。 A。
Id(c => c.SystemId);
Version(c => c.Version);
References(c => c.x).Cascade.All();
References(c => c.y).Cascade.All();
HasMany<B>(Reveal.Property<A>("_B"))
.AsBag()
.Cascade.AllDeleteOrphan()
.Not.LazyLoad()
.Inverse()
.Cache.ReadWrite().IncludeAll();
HasMany<C>(Reveal.Property<A>("_C"))
.AsBag()
.Cascade.AllDeleteOrphan()
.LazyLoad()
.Inverse()
.Cache.ReadWrite().IncludeAll();
我不想对映射文件进行更改,并且想急切地加载整个实体 即我应该得到一个 A 的列表,其中将有 B 和 C 的列表,其引用属性也将被急切地加载
I have an entity A which HasMany entities B and entities C. All entities A, B and C have some references x,y and z which should be loaded eagerly.
I want to read from the database all entities A, and load the collections of B and C eagerly using criteria API.
So far, I am able to fetch the references in 'A' eagerly. But when the collections are loaded, the references within them are lazily loaded.
Here is how I do it
AllEntities_A =
_session.CreateCriteria(typeof(A))
.SetFetchMode("x", FetchMode.Eager)
.SetFetchMode("y", FetchMode.Eager)
.List<A>().AsQueryable();
The mapping of entity A using Fluent is as shown below. _B and _C are private ILists for B & C respectively in A.
Id(c => c.SystemId);
Version(c => c.Version);
References(c => c.x).Cascade.All();
References(c => c.y).Cascade.All();
HasMany<B>(Reveal.Property<A>("_B"))
.AsBag()
.Cascade.AllDeleteOrphan()
.Not.LazyLoad()
.Inverse()
.Cache.ReadWrite().IncludeAll();
HasMany<C>(Reveal.Property<A>("_C"))
.AsBag()
.Cascade.AllDeleteOrphan()
.LazyLoad()
.Inverse()
.Cache.ReadWrite().IncludeAll();
I don't want to make changes to the mapping file, and would like to load the entire entity A eagerly. i.e. I should get a List of A's where there will be List of B's and C's whose reference properties will also be loaded eagerly
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在尝试在这里做笛卡尔积。我认为 NHibernate 需要将关系映射为集合而不是包来做到这一点,因为包允许重复。
不管怎样,笛卡尔积的效率很低。请改用多重查询或未来查询。
请参阅:
You're trying to do a cartesian product here. I think NHibernate requires mapping the relations as sets instead of bags to do that, since bags allow duplicates.
Anyway, cartesian products are very inefficient. Use a multi-query or future queries instead.
See: