I need to know what is the difference between JoinQueryOver and JoinAlias, and when to use each?
Asked
Active
Viewed 4.1k times
2 Answers
111
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.
-
7Note that in the second example you have to declare the aliases `Kitten kittenAlias = null;` and `Cat catAlias = null;` earlier. I find it messy, so I don't use `JoinAlias` unless it's necessary. – foka Jun 06 '14 at 11:41
-
Thank you @foka for clarifying this. I missed this and was wondering why it didn't work. – Mario Tacke Oct 20 '14 at 17:32
14
QueryOver Series - Part 2: Basics and Joining by Andrew Whitaker gives a very good explanation:
Summary:
IQueryOveris a generic type with two type parametersTRootandTSubType.Selectoperates onTRootwhile other QueryOver methods operate onTSubType.TRootstays the same as you’re building a query, butTSubTypechanges when you join usingJoinQueryOverJoinQueryOverandJoinAliasadd joins to your query.JoinAliasdoesn’t changeTSubType, butJoinQueryOverdoes.- You can use aliases when building a query to refer to properties that don’t belong to
TRootorTSubType
Community
- 1
- 1
Michał Powaga
- 22,561
- 8
- 51
- 62