@@ -102,5 +102,66 @@ public async System.Threading.Tasks.Task ElaboratedModelAsync()
102102 Assert . DoesNotThrowAsync ( ( ) => { return tran . CommitAsync ( ) ; } ) ;
103103 }
104104 }
105+
106+ // #1338
107+ [ Test ]
108+ public async System . Threading . Tasks . Task InsertShouldNotInitializeManyToOneProxyAsync ( )
109+ {
110+ var person = new Person { Name = "AnimalOwner" } ;
111+ using ( var s = OpenSession ( ) )
112+ using ( var t = s . BeginTransaction ( ) )
113+ {
114+ await ( s . SaveAsync ( person ) ) ;
115+ await ( t . CommitAsync ( ) ) ;
116+ }
117+ await ( Sfi . EvictAsync ( typeof ( Person ) ) ) ;
118+
119+ using ( var s = OpenSession ( ) )
120+ using ( var t = s . BeginTransaction ( ) )
121+ {
122+ var personProxy = await ( s . LoadAsync < Person > ( person . Id ) ) ;
123+ Assert . That ( NHibernateUtil . IsInitialized ( personProxy ) , Is . False , "Person proxy already initialized after load" ) ;
124+
125+ await ( s . SaveAsync ( new Cat { Name = "Felix" , Owner = personProxy } ) ) ;
126+ await ( s . SaveAsync ( new Cat { Name = "Loustic" , Owner = personProxy } ) ) ;
127+ Assert . That ( NHibernateUtil . IsInitialized ( personProxy ) , Is . False , "Person proxy initialized after saves" ) ;
128+ await ( t . CommitAsync ( ) ) ;
129+ Assert . That ( NHibernateUtil . IsInitialized ( personProxy ) , Is . False , "Person proxy initialized after commit" ) ;
130+ }
131+ }
132+
133+ [ Test ]
134+ public async System . Threading . Tasks . Task InsertShouldNotInitializeOneToManyProxyAsync ( )
135+ {
136+ var cat = new Cat { Name = "Felix" } ;
137+ using ( var s = OpenSession ( ) )
138+ using ( var t = s . BeginTransaction ( ) )
139+ {
140+ await ( s . SaveAsync ( cat ) ) ;
141+ await ( t . CommitAsync ( ) ) ;
142+ }
143+ await ( Sfi . EvictAsync ( typeof ( Cat ) ) ) ;
144+
145+ using ( var s = OpenSession ( ) )
146+ using ( var t = s . BeginTransaction ( ) )
147+ {
148+ var catProxy = await ( s . LoadAsync < Cat > ( cat . Id ) ) ;
149+ Assert . That ( NHibernateUtil . IsInitialized ( catProxy ) , Is . False , "Cat proxy already initialized after load" ) ;
150+
151+ var owner = new Person { Name = "AnimalOwner" } ;
152+ owner . AnimalsGeneric . Add ( catProxy ) ;
153+ // Following assert would fail if the collection was changed for a set.
154+ Assert . That ( NHibernateUtil . IsInitialized ( catProxy ) , Is . False , "Cat proxy initialized after collection add" ) ;
155+ await ( s . SaveAsync ( owner ) ) ;
156+ Assert . That ( NHibernateUtil . IsInitialized ( catProxy ) , Is . False , "Cat proxy initialized after save" ) ;
157+ await ( t . CommitAsync ( ) ) ;
158+ Assert . That ( NHibernateUtil . IsInitialized ( catProxy ) , Is . False , "Cat proxy initialized after commit" ) ;
159+ // The collection being inverse, the cat owner is not actually set in this test, but that is enough
160+ // to check the trouble. The ordering logic does not short-circuit on inverse collections. (It could
161+ // be an optimization, but it may cause regressions for some edge case mappings, like one having an
162+ // inverse one-to-many with no matching many-to-one but a basic type property for the foreign key
163+ // instead.)
164+ }
165+ }
105166 }
106- }
167+ }
0 commit comments