@@ -4026,7 +4026,23 @@ public void ProcessInsertGeneratedProperties(object id, object entity, object[]
4026
4026
{
4027
4027
throw new AssertionFailure ( "no insert-generated properties" ) ;
4028
4028
}
4029
- ProcessGeneratedProperties ( id , entity , state , session , sqlInsertGeneratedValuesSelectString , PropertyInsertGenerationInclusions ) ;
4029
+
4030
+ session . Batcher . ExecuteBatch ( ) ; //force immediate execution of the insert
4031
+
4032
+ if ( loaderName == null )
4033
+ {
4034
+ ProcessGeneratedPropertiesWithGeneratedSql ( id , entity , state , session , sqlInsertGeneratedValuesSelectString , PropertyInsertGenerationInclusions ) ;
4035
+ }
4036
+ else
4037
+ {
4038
+ ProcessGeneratedPropertiesWithLoader ( id , entity , session ) ;
4039
+
4040
+ // The loader has added the entity to the first-level cache. We must remove
4041
+ // the entity from the first-level cache to avoid problems in the Save or SaveOrUpdate
4042
+ // event listeners, which don't expect the entity to already be present in the
4043
+ // first-level cache.
4044
+ session . PersistenceContext . RemoveEntity ( session . GenerateEntityKey ( id , this ) ) ;
4045
+ }
4030
4046
}
4031
4047
4032
4048
public void ProcessUpdateGeneratedProperties ( object id , object entity , object [ ] state , ISessionImplementor session )
@@ -4035,14 +4051,25 @@ public void ProcessUpdateGeneratedProperties(object id, object entity, object[]
4035
4051
{
4036
4052
throw new AssertionFailure ( "no update-generated properties" ) ;
4037
4053
}
4038
- ProcessGeneratedProperties ( id , entity , state , session , sqlUpdateGeneratedValuesSelectString , PropertyUpdateGenerationInclusions ) ;
4054
+
4055
+ session . Batcher . ExecuteBatch ( ) ; //force immediate execution of the update
4056
+
4057
+ if ( loaderName == null )
4058
+ {
4059
+ ProcessGeneratedPropertiesWithGeneratedSql ( id , entity , state , session , sqlUpdateGeneratedValuesSelectString , PropertyUpdateGenerationInclusions ) ;
4060
+ }
4061
+ else
4062
+ {
4063
+ // Remove entity from first-level cache to ensure that loader fetches fresh data from database.
4064
+ // The loader will ensure that the same entity is added back to the first-level cache.
4065
+ session . PersistenceContext . RemoveEntity ( session . GenerateEntityKey ( id , this ) ) ;
4066
+ ProcessGeneratedPropertiesWithLoader ( id , entity , session ) ;
4067
+ }
4039
4068
}
4040
4069
4041
- private void ProcessGeneratedProperties ( object id , object entity , object [ ] state ,
4042
- ISessionImplementor session , SqlString selectionSQL , ValueInclusion [ ] includeds )
4070
+ private void ProcessGeneratedPropertiesWithGeneratedSql ( object id , object entity , object [ ] state ,
4071
+ ISessionImplementor session , SqlString selectionSQL , ValueInclusion [ ] generationInclusions )
4043
4072
{
4044
- session . Batcher . ExecuteBatch ( ) ; //force immediate execution of the insert
4045
-
4046
4073
using ( new SessionIdLoggingContext ( session . SessionId ) )
4047
4074
try
4048
4075
{
@@ -4060,7 +4087,7 @@ private void ProcessGeneratedProperties(object id, object entity, object[] state
4060
4087
}
4061
4088
for ( int i = 0 ; i < PropertySpan ; i ++ )
4062
4089
{
4063
- if ( includeds [ i ] != ValueInclusion . None )
4090
+ if ( generationInclusions [ i ] != ValueInclusion . None )
4064
4091
{
4065
4092
object hydratedState = PropertyTypes [ i ] . Hydrate ( rs , GetPropertyAliases ( string . Empty , i ) , session , entity ) ;
4066
4093
state [ i ] = PropertyTypes [ i ] . ResolveIdentifier ( hydratedState , session , entity ) ;
@@ -4087,6 +4114,24 @@ private void ProcessGeneratedProperties(object id, object entity, object[] state
4087
4114
}
4088
4115
}
4089
4116
4117
+ private void ProcessGeneratedPropertiesWithLoader ( object id , object entity , ISessionImplementor session )
4118
+ {
4119
+ var query = ( AbstractQueryImpl ) session . GetNamedQuery ( loaderName ) ;
4120
+ if ( query . HasNamedParameters )
4121
+ {
4122
+ query . SetParameter ( query . NamedParameters [ 0 ] , id , this . IdentifierType ) ;
4123
+ }
4124
+ else
4125
+ {
4126
+ query . SetParameter ( 0 , id , this . IdentifierType ) ;
4127
+ }
4128
+ query . SetOptionalId ( id ) ;
4129
+ query . SetOptionalEntityName ( this . EntityName ) ;
4130
+ query . SetOptionalObject ( entity ) ;
4131
+ query . SetFlushMode ( FlushMode . Never ) ;
4132
+ query . List ( ) ;
4133
+ }
4134
+
4090
4135
public bool HasSubselectLoadableCollections
4091
4136
{
4092
4137
get { return hasSubselectLoadableCollections ; }
0 commit comments