开发者

Nhibernate QueryOver - why do I have to specify JoinQueryOver

开发者 https://www.devze.com 2023-02-10 17:23 出处:网络
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:

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.List开发者_运维问答ings);

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.


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)
0

精彩评论

暂无评论...
验证码 换一张
取 消