I need to know what is the difference between JoinQueryOver and JoinAlias, and when to use each?
JoinAlias is a lambda expression that creates an alias.
QueryOver is a strongly-typed version of Criteria, and is more NHibernate specific. Pretty much anything you can do in ICriteria can be done with QueryOver.
Functionally they do the same thing, create a join to another entity. The only difference is what they return. JoinQueryOver returns a new QueryOver with the current entity being the entity joined, while JoinAlias returns the original QueryOver that has the current entity as the original root entity.
Whichever one you use is a matter of personal taste: (from http://nhibernate.info/doc/nh/en/index.html#queryqueryover)
IQueryOver<Cat,Kitten> catQuery = session.QueryOver<Cat>() .JoinQueryOver<Kitten>(c => c.Kittens) .Where(k => k.Name == "Tiddles");
and
Cat catAlias = null; Kitten kittenAlias = null; IQueryOver<Cat,Cat> catQuery = session.QueryOver<Cat>(() => catAlias) .JoinAlias(() => catAlias.Kittens, () => kittenAlias) .Where(() => kittenAlias.Name == "Tiddles");
Are functionally the same. Note how the kittenAlias is expressly referenced in the second query.
QueryOver Series - Part 2: Basics and Joining by Andrew Whitaker gives a very good explanation:
Summary:
IQueryOver
is a generic type with two type parametersTRoot
andTSubType
.Select
operates onTRoot
while other QueryOver methods operate onTSubType
.TRoot
stays the same as you’re building a query, butTSubType
changes when you join usingJoinQueryOver
JoinQueryOver
andJoinAlias
add joins to your query.JoinAlias
doesn’t changeTSubType
, butJoinQueryOver
does.- You can use aliases when building a query to refer to properties that don’t belong to
TRoot
orTSubType
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With