Why does NHIbernate (Fluent) still execute queries for my Noop 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 开发者_如何学C== 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).
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).
I suspect this is a hack, but if I add LazyLoad() to the mapping, it prevents the second unwanted query from running:
HasManyToMany(x => x.Projects).Table("UsersProjects").ParentKeyColumn("UserID").LazyLoad().Access.None();
精彩评论