@@ -138,7 +138,7 @@ private bool IsNullifiable(string entityName, object obj)
138
138
EntityEntry entityEntry = session . PersistenceContext . GetEntry ( obj ) ;
139
139
if ( entityEntry == null )
140
140
{
141
- return IsTransient ( entityName , obj , null , session ) ;
141
+ return IsTransientSlow ( entityName , obj , session ) ;
142
142
}
143
143
else
144
144
{
@@ -151,31 +151,25 @@ private bool IsNullifiable(string entityName, object obj)
151
151
/// Is this instance persistent or detached?
152
152
/// </summary>
153
153
/// <remarks>
154
- /// If <paramref name="assumed"/> is non-null, don't hit the database to make the
155
- /// determination, instead assume that value; the client code must be
156
- /// prepared to "recover" in the case that this assumed result is incorrect.
154
+ /// Hit the database to make the determination.
157
155
/// </remarks>
158
- public static bool IsNotTransient ( string entityName , System . Object entity , bool ? assumed , ISessionImplementor session )
156
+ public static bool IsNotTransientSlow ( string entityName , object entity , ISessionImplementor session )
159
157
{
160
158
if ( entity . IsProxy ( ) )
161
159
return true ;
162
160
if ( session . PersistenceContext . IsEntryFor ( entity ) )
163
161
return true ;
164
- return ! IsTransient ( entityName , entity , assumed , session ) ;
162
+ return ! IsTransientSlow ( entityName , entity , session ) ;
165
163
}
166
164
167
165
/// <summary>
168
166
/// Is this instance, which we know is not persistent, actually transient?
169
- /// If <tt>assumed</tt> is non-null, don't hit the database to make the
170
- /// determination, instead assume that value; the client code must be
171
- /// prepared to "recover" in the case that this assumed result is incorrect.
167
+ /// Don't hit the database to make the determination, instead return null;
172
168
/// </summary>
173
169
/// <remarks>
174
- /// If <paramref name="assumed"/> is non-null, don't hit the database to make the
175
- /// determination, instead assume that value; the client code must be
176
- /// prepared to "recover" in the case that this assumed result is incorrect.
170
+ /// Don't hit the database to make the determination, instead return null;
177
171
/// </remarks>
178
- public static bool IsTransient ( string entityName , object entity , bool ? assumed , ISessionImplementor session )
172
+ public static bool ? IsTransientFast ( string entityName , object entity , ISessionImplementor session )
179
173
{
180
174
if ( Equals ( Intercept . LazyPropertyInitializer . UnfetchedProperty , entity ) )
181
175
{
@@ -185,27 +179,33 @@ public static bool IsTransient(string entityName, object entity, bool? assumed,
185
179
}
186
180
187
181
// let the interceptor inspect the instance to decide
188
- bool ? isUnsaved = session . Interceptor . IsTransient ( entity ) ;
189
- if ( isUnsaved . HasValue )
190
- return isUnsaved . Value ;
191
-
192
182
// let the persister inspect the instance to decide
193
- IEntityPersister persister = session . GetEntityPersister ( entityName , entity ) ;
194
- isUnsaved = persister . IsTransient ( entity , session ) ;
195
- if ( isUnsaved . HasValue )
196
- return isUnsaved . Value ;
183
+ return session . Interceptor . IsTransient ( entity ) ??
184
+ session . GetEntityPersister ( entityName , entity ) . IsTransient ( entity , session ) ;
185
+ }
197
186
198
- // we use the assumed value, if there is one, to avoid hitting
199
- // the database
200
- if ( assumed . HasValue )
201
- return assumed . Value ;
187
+ /// <summary>
188
+ /// Is this instance, which we know is not persistent, actually transient?
189
+ /// </summary>
190
+ /// <remarks>
191
+ /// Hit the database to make the determination.
192
+ /// </remarks>
193
+ public static bool IsTransientSlow ( string entityName , object entity , ISessionImplementor session )
194
+ {
195
+ return IsTransientFast ( entityName , entity , session ) ??
196
+ HasDbSnapshot ( entityName , entity , session ) ;
197
+ }
202
198
199
+ static bool HasDbSnapshot ( string entityName , object entity , ISessionImplementor session )
200
+ {
201
+ IEntityPersister persister = session . GetEntityPersister ( entityName , entity ) ;
203
202
if ( persister . IdentifierGenerator is Assigned )
204
203
{
205
204
// When using assigned identifiers we cannot tell if an entity
206
205
// is transient or detached without querying the database.
207
206
// This could potentially cause Select N+1 in cascaded saves, so warn the user.
208
- log . Warn ( "Unable to determine if " + entity . ToString ( )
207
+ log . Warn (
208
+ "Unable to determine if " + entity . ToString ( )
209
209
+ " with assigned identifier " + persister . GetIdentifier ( entity )
210
210
+ " is transient or detached; querying the database."
211
211
+ " Use explicit Save() or Update() in session to prevent this." ) ;
@@ -247,7 +247,7 @@ public static object GetEntityIdentifierIfNotUnsaved(string entityName, object e
247
247
return entity ;
248
248
/**********************************************/
249
249
250
- if ( IsTransient ( entityName , entity , false , session ) )
250
+ if ( IsTransientFast ( entityName , entity , session ) . GetValueOrDefault ( ) )
251
251
{
252
252
/***********************************************/
253
253
// TODO NH verify the behavior of NH607 test
0 commit comments