为什么 NHIbernate (Fluent) 仍然对我的 Noop 属性执行查询?
我有一个与项目具有多对多关系的用户对象。在我的用户映射中,我有这样的:
HasManyToMany(x => x.Projects).Table("UsersProjects").ParentKeyColumn("UserID").Access.None();
当我运行一个简单的按 id 获取查询时:
session.QueryOver<User>()
.Where(x => x.PrimaryID == id)
.Take(1).SingleOrDefault();
我运行两个查询 - 第一个是按 id 获取的查询,第二个是获取项目列表的查询。
我认为 noop 属性的目的是让 NHibernate 能够意识到一种关系,但实际上并不填充该属性......? 有趣的是,查询后 Projects 属性为 null - 因此未设置该属性(使第二个查询更加冗余!)
我正在使用 NHibernate v3.1.0.4000 和 FluentNHibernate v1.2.0.712
编辑
我已经做了一些测试并确定这不是使用流畅查询接口特有的问题。此外,当我将映射转储到 hbm 文件时,此属性的映射如下:
<set access="none" name="Projects" table="UsersProjects">
<key>
<column name="UserID" />
</key>
<many-to-many class="Project">
<column name="ProjectID" />
</many-to-many>
</set>
这看起来像我所期望的 (http://ayende.com/blog/4054/nhibernate-query-only-properties)。
I have a user object that has a many to many relationship with project. In my user mapping, I have this:
HasManyToMany(x => x.Projects).Table("UsersProjects").ParentKeyColumn("UserID").Access.None();
When I run a simple get by id query:
session.QueryOver<User>()
.Where(x => x.PrimaryID == id)
.Take(1).SingleOrDefault();
I get two queries being run - the first is the query to do the get by id, the second is the query to get the list of Projects.
I thought the point of the noop property was so that NHibernate could be aware of a relationship, but not actually populate the property...?
Interestingly, the Projects property is null after the query - so the property isn't being set (making the second query even more redundant!)
I'm using NHibernate v3.1.0.4000 and FluentNHibernate v1.2.0.712
Edit
I've done a bit of testing and determined that this is not a problem specific to using the fluent query interface. In addition, when I dump out my mappings to hbm files, the mapping for this property is as follows:
<set access="none" name="Projects" table="UsersProjects">
<key>
<column name="UserID" />
</key>
<many-to-many class="Project">
<column name="ProjectID" />
</many-to-many>
</set>
This looks like what I'd expect (http://ayende.com/blog/4054/nhibernate-query-only-properties).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Access=none
属性指的是 POCO 中集合的访问级别;不在你的查询中。 (访问权限可以是“财产”、“字段”、“公共字段”等)。因此它与您的收藏的处理方式无关。请参阅此处和此处
使用lazy=true将阻止加载您的集合,直到您引用它(据我所知,这就是您想要的)。
the
Access=none
property refers to the access level of the collection in your POCO; not in your queries. (access can be 'property', 'field', 'public field' etc.). so it has no bearing on how your collection is handled.see here and here
using
lazy=true
would prevent your collection from being loaded until you reference it (which, as I understand it, is what you want).我怀疑这是一个黑客攻击,但如果我将 LazyLoad() 添加到映射中,它会阻止第二个不需要的查询运行:
I suspect this is a hack, but if I add LazyLoad() to the mapping, it prevents the second unwanted query from running: