23
23
*/
24
24
package org .hibernate .loader .plan .exec .internal ;
25
25
26
+ import org .hibernate .AssertionFailure ;
26
27
import org .hibernate .engine .FetchStyle ;
27
28
import org .hibernate .engine .FetchTiming ;
28
29
import org .hibernate .engine .spi .SessionFactoryImplementor ;
@@ -284,28 +285,43 @@ private void renderCollectionJoin(Join join, JoinFragment joinFragment) {
284
285
// For many-to-many, the follow-on join will join to the associated entity element table. For one-to-many,
285
286
// the collection table is the associated entity table, so the follow-on join will not be rendered..
286
287
288
+ // currently we do not explicitly track the joins under the CollectionQuerySpace to know which is
289
+ // the element join and which is the index join (maybe we should?).
290
+
291
+ JoinDefinedByMetadata collectionElementJoin = null ;
292
+ JoinDefinedByMetadata collectionIndexJoin = null ;
293
+ for ( Join collectionJoin : rightHandSide .getJoins () ) {
294
+ if ( JoinDefinedByMetadata .class .isInstance ( collectionJoin ) ) {
295
+ final JoinDefinedByMetadata collectionJoinDefinedByMetadata = (JoinDefinedByMetadata ) collectionJoin ;
296
+ if ( CollectionPropertyNames .COLLECTION_ELEMENTS .equals ( collectionJoinDefinedByMetadata .getJoinedPropertyName () ) ) {
297
+ if ( collectionElementJoin != null ) {
298
+ throw new AssertionFailure (
299
+ String .format (
300
+ "More than one element join defined for: %s" ,
301
+ rightHandSide .getCollectionPersister ().getRole ()
302
+ )
303
+ );
304
+ }
305
+ collectionElementJoin = collectionJoinDefinedByMetadata ;
306
+ }
307
+ if ( CollectionPropertyNames .COLLECTION_INDICES .equals ( collectionJoinDefinedByMetadata .getJoinedPropertyName () ) ) {
308
+ if ( collectionIndexJoin != null ) {
309
+ throw new AssertionFailure (
310
+ String .format (
311
+ "More than one index join defined for: %s" ,
312
+ rightHandSide .getCollectionPersister ().getRole ()
313
+ )
314
+ );
315
+ }
316
+ collectionIndexJoin = collectionJoinDefinedByMetadata ;
317
+ }
318
+ }
319
+ }
320
+
287
321
if ( rightHandSide .getCollectionPersister ().isOneToMany ()
288
322
|| rightHandSide .getCollectionPersister ().isManyToMany () ) {
289
323
// relatedly, for collections with entity elements (one-to-many, many-to-many) we need to register the
290
324
// sql aliases to use for the entity.
291
- //
292
- // currently we do not explicitly track the joins under the CollectionQuerySpace to know which is
293
- // the element join and which is the index join (maybe we should?). Another option here is to have the
294
- // "collection join" act as the entity element join in this case (much like I do with entity identifiers).
295
- // The difficulty there is that collections can theoretically could be multiple joins in that case (one
296
- // for element, one for index). However, that's a bit of future-planning as today Hibernate does not
297
- // properly deal with the index anyway in terms of allowing dynamic fetching across a collection index...
298
- //
299
- // long story short, for now we'll use an assumption that the last join in the CollectionQuerySpace is the
300
- // element join (that's how the joins are built as of now..)
301
- //
302
- // todo : remove this assumption ^^; maybe we make CollectionQuerySpace "special" and rather than have it
303
- // hold a list of joins, we have it expose the 2 (index, element) separately.
304
-
305
- Join collectionElementJoin = null ;
306
- for ( Join collectionJoin : rightHandSide .getJoins () ) {
307
- collectionElementJoin = collectionJoin ;
308
- }
309
325
if ( collectionElementJoin == null ) {
310
326
throw new IllegalStateException (
311
327
String .format (
@@ -329,6 +345,24 @@ private void renderCollectionJoin(Join join, JoinFragment joinFragment) {
329
345
);
330
346
}
331
347
348
+ if ( rightHandSide .getCollectionPersister ().hasIndex () &&
349
+ rightHandSide .getCollectionPersister ().getIndexType ().isEntityType () ) {
350
+ // for collections with entity index we need to register the
351
+ // sql aliases to use for the entity.
352
+ if ( collectionIndexJoin == null ) {
353
+ throw new IllegalStateException (
354
+ String .format (
355
+ "Could not locate collection index join within collection join [%s : %s]" ,
356
+ rightHandSide .getUid (),
357
+ rightHandSide .getCollectionPersister ()
358
+ )
359
+ );
360
+ }
361
+ aliasResolutionContext .generateEntityReferenceAliases (
362
+ collectionIndexJoin .getRightHandSide ().getUid (),
363
+ rightHandSide .getCollectionPersister ().getIndexDefinition ().toEntityDefinition ().getEntityPersister ()
364
+ );
365
+ }
332
366
addJoins (
333
367
join ,
334
368
joinFragment ,
0 commit comments