NHibernate:无法成功预加载
我使用的是用 NH3 重新编译的 NH 3.0 和 FNH 1.1。
我有一个用户模型,我希望在加载它时始终检索其配置文件。我使用 NH3 的 linq 提供程序,但无法使用其 Fetch 方法(因为我的存储库隐藏了 NHibernate 并返回 IQueryable,并且在查询中调用了 ToPagedList,因此阻止我将 Fetch 作为查询的最后一个调用)。
在 UserMap 中我设置:
HasOne(x => x.Profile)
.Not.LazyLoad()
.Cascade.All();
但是将 LazyLoad 设置为 OFF 并没有帮助。我也玩过获取模式。
我的期望是,如果我定义此映射,那么我什至不必告诉 Linq 我希望在请求 User 实体时获取 Profile。 Linq 应该尊重映射,不是吗?
I'm using NH 3.0 and FNH 1.1 recompiled with NH3.
I have a user model where I want to always retrieve its profile when loading it. I use the linq provider from NH3 but can't use its Fetch method (because of my repository that hides NHibernate and returns an IQueryable and the fact that ToPagedList is called on the query therefore preventing me to put Fetch as the last call of the query).
In the UserMap I set:
HasOne(x => x.Profile)
.Not.LazyLoad()
.Cascade.All();
But setting LazyLoad to OFF does not help. I played with the fetch mode too.
My expectation is that if I define this mapping, then I should not even have to tell Linq that I want Profile to be fetched when the User entity is requested. Linq should honour the mapping, no?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我也遇到这个问题,不幸的是我认为这是设计使然。 NHibernate 3.0 Linq 提供程序在幕后使用 HQL,而 HQL 在这方面不支持您的映射。例如,如果你这样做了
您只会获得所有配置文件的列表,即使您的映射具有 external-join=true,您的用户类也不会加入。
如果您使用的是使用 Critera API 的旧 NHibernate.Linq 提供程序,或直接使用 Criteria API:
您将返回与用户外部连接的所有配置文件的列表,正如您的映射文件所请求的那样。
现在我不知道为什么 HQL 支持的 linq 提供程序不尊重您的映射(如果有人知道解决此问题的方法,请发帖),但我相信这就是您看到此行为的原因。
I am having this problem as well, and unfortunately I think it is by design. The NHibernate 3.0 Linq provider uses HQL under the covers, and HQL doesn't honor your mappings in this respect. For example, if you did
You would only get a list of all profiles and your user class would not join even if your mapping has outer-join=true.
If you were using the old NHibernate.Linq provider that used the Critera API, or the Criteria API directly:
you would get back a list of all profiles left outer joined with users, just as your mapping file requested.
Now I don't know why the HQL backed linq provider doesn't honor your mappings (and if anyone knows a way around this, please post), but I believe that is why you are seeing this behavior.