@@ -28,7 +28,26 @@ public void CanJoinNotAssociatedEntity()
2828 EntityComplex entityComplex = null ;
2929 EntityWithNoAssociation root = null ;
3030 root = session . QueryOver < EntityWithNoAssociation > ( ( ) => root )
31- . JoinEntityAlias ( ( ) => entityComplex , JoinType . InnerJoin , Restrictions . Where ( ( ) => root . Complex1Id == entityComplex . Id ) ) . Take ( 1 )
31+ . JoinEntityAlias ( ( ) => entityComplex , Restrictions . Where ( ( ) => root . Complex1Id == entityComplex . Id ) , JoinType . InnerJoin ) . Take ( 1 )
32+ . SingleOrDefault ( ) ;
33+ entityComplex = session . Load < EntityComplex > ( root . Complex1Id ) ;
34+
35+ Assert . That ( NHibernateUtil . IsInitialized ( entityComplex ) , Is . True ) ;
36+ Assert . That ( sqlLog . Appender . GetEvents ( ) . Length , Is . EqualTo ( 1 ) , "Only one SQL select is expected" ) ;
37+ }
38+ }
39+
40+ //check JoinEntityAlias - JoinAlias analog for entity join
41+ [ Test ]
42+ public void CanJoinNotAssociatedEntity_Expression ( )
43+ {
44+ using ( var sqlLog = new SqlLogSpy ( ) )
45+ using ( var session = OpenSession ( ) )
46+ {
47+ EntityComplex entityComplex = null ;
48+ EntityWithNoAssociation root = null ;
49+ root = session . QueryOver ( ( ) => root )
50+ . JoinEntityAlias ( ( ) => entityComplex , ( ) => root . Complex1Id == entityComplex . Id ) . Take ( 1 )
3251 . SingleOrDefault ( ) ;
3352 entityComplex = session . Load < EntityComplex > ( root . Complex1Id ) ;
3453
@@ -47,7 +66,7 @@ public void CanJoinEntityQueryOver()
4766 EntityComplex ejQueryOver = null ;
4867 EntityWithNoAssociation root = null ;
4968 root = session . QueryOver < EntityWithNoAssociation > ( ( ) => root )
50- . JoinEntityQueryOver ( ( ) => ejQueryOver , JoinType . InnerJoin , Restrictions . Where ( ( ) => root . Complex1Id == ejQueryOver . Id ) )
69+ . JoinEntityQueryOver ( ( ) => ejQueryOver , Restrictions . Where ( ( ) => root . Complex1Id == ejQueryOver . Id ) , JoinType . InnerJoin )
5170 . Take ( 1 )
5271 . SingleOrDefault ( ) ;
5372 ejQueryOver = session . Load < EntityComplex > ( root . Complex1Id ) ;
@@ -67,7 +86,7 @@ public void CanQueryOverForAssociationInNotAssociatedEntity()
6786 EntityComplex ejComplex = null ;
6887 EntityWithNoAssociation root = null ;
6988 root = session . QueryOver < EntityWithNoAssociation > ( ( ) => root )
70- . JoinEntityQueryOver ( ( ) => ejComplex , JoinType . InnerJoin , Restrictions . Where ( ( ) => root . Complex1Id == ejComplex . Id ) )
89+ . JoinEntityQueryOver ( ( ) => ejComplex , Restrictions . Where ( ( ) => root . Complex1Id == ejComplex . Id ) , JoinType . InnerJoin )
7190 . JoinQueryOver ( ej => ej . Child1 )
7291 . Take ( 1 )
7392 . SingleOrDefault ( ) ;
@@ -90,7 +109,7 @@ public void SimpleProjectionForEntityJoin()
90109 EntityComplex ejComplex = null ;
91110 EntityWithNoAssociation root = null ;
92111 var name = session . QueryOver < EntityWithNoAssociation > ( ( ) => root )
93- . JoinEntityQueryOver ( ( ) => ejComplex , JoinType . InnerJoin , Restrictions . Where ( ( ) => root . Complex1Id == ejComplex . Id ) )
112+ . JoinEntityQueryOver ( ( ) => ejComplex , Restrictions . Where ( ( ) => root . Complex1Id == ejComplex . Id ) , JoinType . InnerJoin )
94113 . Select ( ( e ) => ejComplex . Name )
95114 . Take ( 1 )
96115 . SingleOrDefault < string > ( ) ;
@@ -113,7 +132,7 @@ public void EntityProjectionForEntityJoin()
113132 var r = session
114133 . QueryOver < EntityComplex > ( ( ) => root )
115134 . JoinAlias ( c => c . SameTypeChild , ( ) => st )
116- . JoinEntityAlias ( ( ) => ejChild1 , JoinType . InnerJoin , Restrictions . Where ( ( ) => ejChild1 . Id == root . Child1 . Id ) )
135+ . JoinEntityAlias ( ( ) => ejChild1 , Restrictions . Where ( ( ) => ejChild1 . Id == root . Child1 . Id ) , JoinType . InnerJoin )
117136 . Select (
118137 Projections . RootEntity ( ) ,
119138 Projections . Entity ( ( ) => st ) ,
@@ -148,10 +167,10 @@ public void MixOfJoinsForAssociatedAndNotAssociatedEntities()
148167 EntitySimpleChild ejLevel2OnEntityComplexForEjLevel1 = null ;
149168 var obj = session
150169 . QueryOver < EntityComplex > ( ( ) => root )
151- . JoinEntityAlias ( ( ) => ejLevel1 , JoinType . LeftOuterJoin , Restrictions . Where ( ( ) => ejLevel1 . Id == root . SameTypeChild . Id && root . Id != null ) )
170+ . JoinEntityAlias ( ( ) => ejLevel1 , Restrictions . Where ( ( ) => ejLevel1 . Id == root . SameTypeChild . Id && root . Id != null ) , JoinType . LeftOuterJoin )
152171 . JoinAlias ( ( ) => ejLevel1 . Child1 , ( ) => customChildForEjLevel1 , JoinType . InnerJoin )
153172 . JoinAlias ( ( ) => ejLevel1 . SameTypeChild , ( ) => entityComplexForEjLevel1 , JoinType . LeftOuterJoin )
154- . JoinEntityAlias ( ( ) => ejLevel2OnEntityComplexForEjLevel1 , JoinType . InnerJoin , Restrictions . Where ( ( ) => entityComplexForEjLevel1 . Id == ejLevel2OnEntityComplexForEjLevel1 . Id ) )
173+ . JoinEntityAlias ( ( ) => ejLevel2OnEntityComplexForEjLevel1 , Restrictions . Where ( ( ) => entityComplexForEjLevel1 . Id == ejLevel2OnEntityComplexForEjLevel1 . Id ) , JoinType . InnerJoin )
155174 . Where ( ( ) => customChildForEjLevel1 . Id != null && ejLevel2OnEntityComplexForEjLevel1 . Id != null )
156175 . Take ( 1 )
157176 . SingleOrDefault < object > ( ) ;
@@ -168,7 +187,7 @@ public void EntityJoinForCompositeKey()
168187 EntityWithNoAssociation root = null ;
169188 root = session
170189 . QueryOver < EntityWithNoAssociation > ( ( ) => root )
171- . JoinEntityAlias ( ( ) => ejComposite , JoinType . InnerJoin , Restrictions . Where ( ( ) => root . Composite1Key1 == ejComposite . Key . Id1 && root . Composite1Key2 == ejComposite . Key . Id2 ) )
190+ . JoinEntityAlias ( ( ) => ejComposite , Restrictions . Where ( ( ) => root . Composite1Key1 == ejComposite . Key . Id1 && root . Composite1Key2 == ejComposite . Key . Id2 ) , JoinType . InnerJoin )
172191 . Take ( 1 ) . SingleOrDefault ( ) ;
173192 var composite = session . Load < EntityWithCompositeId > ( _entityWithCompositeId . Key ) ;
174193
@@ -188,7 +207,7 @@ public void NullLeftEntityJoin()
188207 EntityWithNoAssociation root = null ;
189208 root = session . QueryOver < EntityWithNoAssociation > ( ( ) => root )
190209 //add some non existent
191- . JoinEntityAlias ( ( ) => ejLeftNull , JoinType . LeftOuterJoin , Restrictions . Where ( ( ) => ejLeftNull . Id == null ) )
210+ . JoinEntityAlias ( ( ) => ejLeftNull , Restrictions . Where ( ( ) => ejLeftNull . Id == null ) , JoinType . LeftOuterJoin )
192211 . Take ( 1 )
193212 . SingleOrDefault ( ) ;
194213
@@ -208,7 +227,7 @@ public void NullLeftEntityJoinWithEntityProjection()
208227 EntityWithNoAssociation root = null ;
209228 var objs = session . QueryOver < EntityWithNoAssociation > ( ( ) => root )
210229 //add some non existent
211- . JoinEntityAlias ( ( ) => ejLeftNull , JoinType . LeftOuterJoin , Restrictions . Where ( ( ) => ejLeftNull . Id == null ) )
230+ . JoinEntityAlias ( ( ) => ejLeftNull , Restrictions . Where ( ( ) => ejLeftNull . Id == null ) , JoinType . LeftOuterJoin )
212231 . Select ( ( e ) => root . AsEntity ( ) , e => ejLeftNull . AsEntity ( ) )
213232 . Take ( 1 )
214233 . SingleOrDefault < object [ ] > ( ) ;
@@ -232,7 +251,7 @@ public void EntityJoinForCustomEntityName()
232251 EntityCustomEntityName ejCustomEntity = null ;
233252 EntityWithNoAssociation root = null ;
234253 root = session . QueryOver < EntityWithNoAssociation > ( ( ) => root )
235- . JoinEntityAlias ( ( ) => ejCustomEntity , JoinType . InnerJoin , Restrictions . Where ( ( ) => ejCustomEntity . Id == root . CustomEntityNameId ) , customEntityName )
254+ . JoinEntityAlias ( ( ) => ejCustomEntity , Restrictions . Where ( ( ) => ejCustomEntity . Id == root . CustomEntityNameId ) , JoinType . InnerJoin , customEntityName )
236255 . Take ( 1 )
237256 . SingleOrDefault ( ) ;
238257
@@ -243,6 +262,61 @@ public void EntityJoinForCustomEntityName()
243262 }
244263 }
245264
265+ [ Test ]
266+ public void EntityJoinFoSubquery_JoinEntityAlias ( )
267+ {
268+ using ( var sqlLog = new SqlLogSpy ( ) )
269+ using ( var session = OpenSession ( ) )
270+ {
271+ EntityComplex ej = null ;
272+ EntityWithNoAssociation root = null ;
273+
274+ EntityComplex ejSub = null ;
275+ EntityWithNoAssociation rootSub = null ;
276+
277+ var subquery = QueryOver . Of < EntityWithNoAssociation > ( ( ) => rootSub )
278+ . JoinEntityAlias ( ( ) => ejSub , ( ) => rootSub . Complex1Id == ejSub . Id )
279+ . Where ( r => ejSub . Name == ej . Name )
280+ . Select ( x => ejSub . Id ) ;
281+
282+ root = session . QueryOver ( ( ) => root )
283+ . JoinEntityAlias ( ( ) => ej , Restrictions . Where ( ( ) => root . Complex1Id == ej . Id ) )
284+ . WithSubquery . WhereExists ( subquery )
285+ . SingleOrDefault ( ) ;
286+ ej = session . Load < EntityComplex > ( root . Complex1Id ) ;
287+
288+ Assert . That ( NHibernateUtil . IsInitialized ( ej ) , Is . True ) ;
289+ Assert . That ( sqlLog . Appender . GetEvents ( ) . Length , Is . EqualTo ( 1 ) , "Only one SQL select is expected" ) ;
290+ }
291+ }
292+
293+ [ Test ]
294+ public void EntityJoinFoSubquery_JoinQueryOver ( )
295+ {
296+ using ( var sqlLog = new SqlLogSpy ( ) )
297+ using ( var session = OpenSession ( ) )
298+ {
299+ EntityComplex ej = null ;
300+ EntityWithNoAssociation root = null ;
301+
302+ EntityComplex ejSub = null ;
303+ EntityWithNoAssociation rootSub = null ;
304+
305+ var subquery = QueryOver . Of < EntityWithNoAssociation > ( ( ) => rootSub )
306+ . JoinEntityQueryOver ( ( ) => ejSub , ( ) => rootSub . Complex1Id == ejSub . Id )
307+ . Where ( x => x . Name == ej . Name )
308+ . Select ( x => ejSub . Id ) ;
309+
310+ root = session . QueryOver ( ( ) => root )
311+ . JoinEntityAlias ( ( ) => ej , Restrictions . Where ( ( ) => root . Complex1Id == ej . Id ) )
312+ . WithSubquery . WhereExists ( subquery )
313+ . SingleOrDefault ( ) ;
314+ ej = session . Load < EntityComplex > ( root . Complex1Id ) ;
315+
316+ Assert . That ( NHibernateUtil . IsInitialized ( ej ) , Is . True ) ;
317+ Assert . That ( sqlLog . Appender . GetEvents ( ) . Length , Is . EqualTo ( 1 ) , "Only one SQL select is expected" ) ;
318+ }
319+ }
246320 #region Test Setup
247321
248322 protected override HbmMapping GetMappings ( )
0 commit comments