@@ -1111,7 +1111,8 @@ boolean hasAnyChildEntityNames(BatchIdentifier batchIdentifier) {
1111
1111
boolean hasParent (BatchIdentifier batchIdentifier ) {
1112
1112
return (
1113
1113
parent == batchIdentifier
1114
- || ( parentEntityNames .contains ( batchIdentifier .getEntityName () ) )
1114
+ || parentEntityNames .contains ( batchIdentifier .getEntityName () )
1115
+ || ( parentEntityNames .contains ( batchIdentifier .getRootEntityName () ) && !this .getEntityName ().equals ( batchIdentifier .getRootEntityName () ) )
1115
1116
|| parent != null && parent .hasParent ( batchIdentifier , new ArrayList <>() )
1116
1117
);
1117
1118
}
@@ -1168,7 +1169,6 @@ public void sort(List<AbstractEntityInsertAction> insertions) {
1168
1169
addParentChildEntityNames ( action , batchIdentifier );
1169
1170
addToBatch ( batchIdentifier , action );
1170
1171
}
1171
- insertions .clear ();
1172
1172
1173
1173
// Examine each entry in the batch list, and build the dependency graph.
1174
1174
for ( int i = 0 ; i < latestBatches .size (); i ++ ) {
@@ -1217,7 +1217,12 @@ public void sort(List<AbstractEntityInsertAction> insertions) {
1217
1217
for ( int j = i + 1 ; j < latestBatches .size (); j ++ ) {
1218
1218
BatchIdentifier nextBatchIdentifier = latestBatches .get ( j );
1219
1219
1220
- if ( batchIdentifier .hasParent ( nextBatchIdentifier ) && !nextBatchIdentifier .hasParent ( batchIdentifier ) ) {
1220
+ if ( batchIdentifier .hasParent ( nextBatchIdentifier ) ) {
1221
+ if ( nextBatchIdentifier .hasParent ( batchIdentifier ) ) {
1222
+ //cycle detected, no need to continue
1223
+ break sort ;
1224
+ }
1225
+
1221
1226
latestBatches .remove ( batchIdentifier );
1222
1227
latestBatches .add ( j , batchIdentifier );
1223
1228
@@ -1235,9 +1240,13 @@ public void sort(List<AbstractEntityInsertAction> insertions) {
1235
1240
}
1236
1241
1237
1242
// Now, rebuild the insertions list. There is a batch for each entry in the name list.
1238
- for ( BatchIdentifier rootIdentifier : latestBatches ) {
1239
- List <AbstractEntityInsertAction > batch = actionBatches .get ( rootIdentifier );
1240
- insertions .addAll ( batch );
1243
+ if ( sorted ) {
1244
+ insertions .clear ();
1245
+
1246
+ for ( BatchIdentifier rootIdentifier : latestBatches ) {
1247
+ List <AbstractEntityInsertAction > batch = actionBatches .get ( rootIdentifier );
1248
+ insertions .addAll ( batch );
1249
+ }
1241
1250
}
1242
1251
}
1243
1252
@@ -1252,17 +1261,27 @@ private void addParentChildEntityNames(AbstractEntityInsertAction action, BatchI
1252
1261
ClassMetadata classMetadata = action .getPersister ().getClassMetadata ();
1253
1262
if ( classMetadata != null ) {
1254
1263
Type [] propertyTypes = classMetadata .getPropertyTypes ();
1264
+ Type identifierType = classMetadata .getIdentifierType ();
1255
1265
1256
1266
for ( int i = 0 ; i < propertyValues .length ; i ++ ) {
1257
1267
Object value = propertyValues [i ];
1258
1268
Type type = propertyTypes [i ];
1259
1269
addParentChildEntityNameByPropertyAndValue ( action , batchIdentifier , type , value );
1260
1270
}
1271
+
1272
+ if ( identifierType .isComponentType () ) {
1273
+ CompositeType compositeType = (CompositeType ) identifierType ;
1274
+ Type [] compositeIdentifierTypes = compositeType .getSubtypes ();
1275
+
1276
+ for ( Type type : compositeIdentifierTypes ) {
1277
+ addParentChildEntityNameByPropertyAndValue ( action , batchIdentifier , type , null );
1278
+ }
1279
+ }
1261
1280
}
1262
1281
}
1263
1282
1264
1283
private void addParentChildEntityNameByPropertyAndValue (AbstractEntityInsertAction action , BatchIdentifier batchIdentifier , Type type , Object value ) {
1265
- if ( type .isEntityType () && value != null ) {
1284
+ if ( type .isEntityType () ) {
1266
1285
final EntityType entityType = (EntityType ) type ;
1267
1286
final String entityName = entityType .getName ();
1268
1287
final String rootEntityName = action .getSession ().getFactory ().getMetamodel ().entityPersister ( entityName ).getRootEntityName ();
@@ -1276,17 +1295,26 @@ private void addParentChildEntityNameByPropertyAndValue(AbstractEntityInsertActi
1276
1295
}
1277
1296
}
1278
1297
else {
1279
- batchIdentifier .getParentEntityNames ().add ( entityName );
1298
+ if ( !batchIdentifier .getEntityName ().equals ( entityName ) ) {
1299
+ batchIdentifier .getParentEntityNames ().add ( entityName );
1300
+ }
1301
+ if ( value != null ) {
1302
+ String valueClass = value .getClass ().getName ();
1303
+ if ( !valueClass .equals ( entityName ) ) {
1304
+ batchIdentifier .getParentEntityNames ().add ( valueClass );
1305
+ }
1306
+ }
1280
1307
if ( !rootEntityName .equals ( entityName ) ) {
1281
1308
batchIdentifier .getParentEntityNames ().add ( rootEntityName );
1282
1309
}
1283
1310
}
1284
1311
}
1285
- else if ( type .isCollectionType () && value != null ) {
1312
+ else if ( type .isCollectionType () ) {
1286
1313
CollectionType collectionType = (CollectionType ) type ;
1287
1314
final SessionFactoryImplementor sessionFactory = ( (SessionImplementor ) action .getSession () )
1288
1315
.getSessionFactory ();
1289
- if ( collectionType .getElementType ( sessionFactory ).isEntityType () ) {
1316
+ if ( collectionType .getElementType ( sessionFactory ).isEntityType () &&
1317
+ !sessionFactory .getMetamodel ().collectionPersister ( collectionType .getRole () ).isManyToMany () ) {
1290
1318
String entityName = collectionType .getAssociatedEntityName ( sessionFactory );
1291
1319
String rootEntityName = action .getSession ().getFactory ().getMetamodel ().entityPersister ( entityName ).getRootEntityName ();
1292
1320
batchIdentifier .getChildEntityNames ().add ( entityName );
0 commit comments