Reputation: 6114
In my NHibernate mappings I have two objects- Listing and User. One user can have many listings, and the (Fluent) mappings are set up as such:
Listing:
References<User>(h => h.User).ForeignKey("fk_UserID").Not.LazyLoad().Fetch.Join().Cascade.SaveUpdate();
User:
HasMany<Listing>(u => u.Listings);
This works fine. However, when I started playing around with QueryOver, I tried:
DbSession.QueryOver<HaveListing>()
.Where(h => h.IsModerated == false)
.And(h => h.User.SpammedStatus == false)
Which fails. This, however, works:
DbSession.QueryOver<HaveListing>()
.Where(h => h.IsModerated == false)
.JoinQueryOver(h => h.User)
.Where(u => u.SpammedStatus == false)
Obviously, using the latter is fine, but I wanted to make sure that I'm not missing something- my relationships are defined in the mappings, so do I really need to specify the join each time in order to do a WHERE on User? It would be a waste to include these joins every time when it isn't necessary.
Upvotes: 4
Views: 3292
Reputation: 52745
QueryOver is not LINQ. It uses Expressions to specify property names, but under the hood it's Criteria, so it's bound to the same rules (all joins are explicit)
Unless you have a compelling reason not to, try the following instead:
DbSession.Query<HaveListing>()
.Where(h => h.IsModerated == false &&
h.User.SpammedStatus == false)
Upvotes: 5