Nhibernate QueryOver - why should I specify JoinQueryOver

In my NHibernate mappings, I have two objects - Listing and User. One user can have many lists, and mappings (Fluent) are configured as such:

Listing:

References<User>(h => h.User).ForeignKey("fk_UserID").Not.LazyLoad().Fetch.Join().Cascade.SaveUpdate(); 

User:

  HasMany<Listing>(u => u.Listings); 

It works great. However, when I started playing with QueryOver, I tried:

 DbSession.QueryOver<HaveListing>() .Where(h => h.IsModerated == false) .And(h => h.User.SpammedStatus == false) 

That does not work. 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 okay, but I wanted to make sure that something was missing - my relationships are defined in the mappings, so do I really need to specify a connection each time to do WHERE is the user? It would be useless to include these associations whenever it is not necessary.

+4
source share
1 answer

QueryOver is not LINQ. It uses expressions to indicate property names, but under the hood it’s Criteria, therefore it is associated with the same rules (all connections are explicit)

If you have no good reason, try instead:

 DbSession.Query<HaveListing>() .Where(h => h.IsModerated == false && h.User.SpammedStatus == false) 
+5
source

Source: https://habr.com/ru/post/1340052/


All Articles