Skip to content

Commit f32c736

Browse files
committed
HHH-8276 - Integrate LoadPlans into UniqueEntityLoader (PoC)
1 parent b10c51e commit f32c736

19 files changed

+504
-329
lines changed

hibernate-core/src/main/java/org/hibernate/loader/entity/BatchingEntityLoaderBuilder.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ public static BatchingEntityLoaderBuilder getBuilder(SessionFactoryImplementor f
4949
}
5050
default: {
5151
return org.hibernate.loader.entity.plan.LegacyBatchingEntityLoaderBuilder.INSTANCE;
52+
// return LegacyBatchingEntityLoaderBuilder.INSTANCE;
5253
}
5354
}
5455
}

hibernate-core/src/main/java/org/hibernate/loader/plan/spi/build/AbstractLoadPlanBuilderStrategy.java

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -309,32 +309,32 @@ public void finishingCollectionElements(CollectionElementDefinition elementDefin
309309
// - the element graph pushed while starting would be popped in finishing/Entity/finishingComposite
310310
}
311311

312-
@Override
313-
public void startingCompositeCollectionElement(CompositeCollectionElementDefinition compositeElementDefinition) {
314-
log.tracef(
315-
"%s Starting composite collection element for (%s)",
316-
StringHelper.repeat( ">>", fetchOwnerStack.size() ),
317-
compositeElementDefinition.getCollectionDefinition().getCollectionPersister().getRole()
318-
);
319-
}
320-
321-
@Override
322-
public void finishingCompositeCollectionElement(CompositeCollectionElementDefinition compositeElementDefinition) {
323-
// pop the current fetch owner, and make sure what we just popped represents this composition
324-
final FetchOwner poppedFetchOwner = popFromStack();
325-
326-
if ( ! CompositeElementGraph.class.isInstance( poppedFetchOwner ) ) {
327-
throw new WalkingException( "Mismatched FetchOwner from stack on pop" );
328-
}
329-
330-
// NOTE : not much else we can really check here atm since on the walking spi side we do not have path
331-
332-
log.tracef(
333-
"%s Finished composite element for : %s",
334-
StringHelper.repeat( "<<", fetchOwnerStack.size() ),
335-
compositeElementDefinition.getCollectionDefinition().getCollectionPersister().getRole()
336-
);
337-
}
312+
// @Override
313+
// public void startingCompositeCollectionElement(CompositeCollectionElementDefinition compositeElementDefinition) {
314+
// log.tracef(
315+
// "%s Starting composite collection element for (%s)",
316+
// StringHelper.repeat( ">>", fetchOwnerStack.size() ),
317+
// compositeElementDefinition.getCollectionDefinition().getCollectionPersister().getRole()
318+
// );
319+
// }
320+
//
321+
// @Override
322+
// public void finishingCompositeCollectionElement(CompositeCollectionElementDefinition compositeElementDefinition) {
323+
// // pop the current fetch owner, and make sure what we just popped represents this composition
324+
// final FetchOwner poppedFetchOwner = popFromStack();
325+
//
326+
// if ( ! CompositeElementGraph.class.isInstance( poppedFetchOwner ) ) {
327+
// throw new WalkingException( "Mismatched FetchOwner from stack on pop" );
328+
// }
329+
//
330+
// // NOTE : not much else we can really check here atm since on the walking spi side we do not have path
331+
//
332+
// log.tracef(
333+
// "%s Finished composite element for : %s",
334+
// StringHelper.repeat( "<<", fetchOwnerStack.size() ),
335+
// compositeElementDefinition.getCollectionDefinition().getCollectionPersister().getRole()
336+
// );
337+
// }
338338

339339
@Override
340340
public void finishingCollection(CollectionDefinition collectionDefinition) {

hibernate-core/src/main/java/org/hibernate/loader/plan2/build/internal/returns/AbstractCompositeEntityIdentifierDescription.java

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -39,21 +39,14 @@ public abstract class AbstractCompositeEntityIdentifierDescription
3939
implements EntityIdentifierDescription, FetchSource, ExpandingEntityIdentifierDescription {
4040

4141
private final EntityReference entityReference;
42-
private final CompositeQuerySpace compositeQuerySpace;
4342

4443
protected AbstractCompositeEntityIdentifierDescription(
4544
EntityReference entityReference,
4645
CompositeQuerySpace compositeQuerySpace,
4746
CompositeType identifierType,
4847
PropertyPath propertyPath) {
49-
super( identifierType, propertyPath );
48+
super( identifierType, compositeQuerySpace, false, propertyPath );
5049
this.entityReference = entityReference;
51-
this.compositeQuerySpace = compositeQuerySpace;
52-
}
53-
54-
@Override
55-
protected String getFetchLeftHandSideUid() {
56-
return compositeQuerySpace.getUid();
5750
}
5851

5952
@Override
@@ -66,11 +59,4 @@ public FetchSource getSource() {
6659
// the source for this (as a Fetch) is the entity reference
6760
return (FetchSource) entityReference.getIdentifierDescription();
6861
}
69-
70-
@Override
71-
public String getQuerySpaceUid() {
72-
return compositeQuerySpace.getUid();
73-
}
74-
75-
7662
}

hibernate-core/src/main/java/org/hibernate/loader/plan2/build/internal/returns/AbstractCompositeFetch.java

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import org.hibernate.loader.plan2.build.spi.LoadPlanBuildingContext;
3636
import org.hibernate.loader.plan2.spi.CollectionFetch;
3737
import org.hibernate.loader.plan2.spi.CompositeFetch;
38+
import org.hibernate.loader.plan2.spi.CompositeQuerySpace;
3839
import org.hibernate.loader.plan2.spi.EntityFetch;
3940
import org.hibernate.loader.plan2.spi.Fetch;
4041
import org.hibernate.loader.plan2.spi.Join;
@@ -56,16 +57,31 @@ public abstract class AbstractCompositeFetch implements CompositeFetch, Expandin
5657
private static final FetchStrategy FETCH_STRATEGY = new FetchStrategy( FetchTiming.IMMEDIATE, FetchStyle.JOIN );
5758

5859
private final CompositeType compositeType;
60+
private final CompositeQuerySpace compositeQuerySpace;
5961
private final PropertyPath propertyPath;
62+
private final boolean allowCollectionFetches;
6063

6164
private List<Fetch> fetches;
6265

63-
protected AbstractCompositeFetch(CompositeType compositeType, PropertyPath propertyPath) {
66+
protected AbstractCompositeFetch(
67+
CompositeType compositeType,
68+
CompositeQuerySpace compositeQuerySpace,
69+
boolean allowCollectionFetches, PropertyPath propertyPath) {
6470
this.compositeType = compositeType;
71+
this.compositeQuerySpace = compositeQuerySpace;
72+
this.allowCollectionFetches = allowCollectionFetches;
6573
this.propertyPath = propertyPath;
6674
}
6775

68-
protected abstract String getFetchLeftHandSideUid();
76+
@SuppressWarnings("UnusedParameters")
77+
protected CompositeQuerySpace resolveCompositeQuerySpace(LoadPlanBuildingContext loadPlanBuildingContext) {
78+
return compositeQuerySpace;
79+
}
80+
81+
@Override
82+
public String getQuerySpaceUid() {
83+
return compositeQuerySpace.getUid();
84+
}
6985

7086
@Override
7187
public void validateFetchPlan(FetchStrategy fetchStrategy, AttributeDefinition attributeDefinition) {
@@ -92,8 +108,8 @@ public EntityFetch buildEntityFetch(
92108
);
93109
}
94110

95-
final ExpandingQuerySpace leftHandSide = (ExpandingQuerySpace) loadPlanBuildingContext.getQuerySpaces().getQuerySpaceByUid(
96-
getFetchLeftHandSideUid()
111+
final ExpandingQuerySpace leftHandSide = (ExpandingQuerySpace) resolveCompositeQuerySpace(
112+
loadPlanBuildingContext
97113
);
98114
final Join join = leftHandSide.addEntityJoin(
99115
attributeDefinition,
@@ -122,11 +138,18 @@ private void addFetch(Fetch fetch) {
122138
public CompositeFetch buildCompositeFetch(
123139
CompositionDefinition attributeDefinition,
124140
LoadPlanBuildingContext loadPlanBuildingContext) {
141+
final ExpandingQuerySpace leftHandSide = (ExpandingQuerySpace) resolveCompositeQuerySpace( loadPlanBuildingContext );
142+
final Join join = leftHandSide.addCompositeJoin(
143+
attributeDefinition,
144+
loadPlanBuildingContext.getQuerySpaces().generateImplicitUid()
145+
);
146+
125147
final NestedCompositeFetchImpl fetch = new NestedCompositeFetchImpl(
126148
this,
127149
attributeDefinition.getType(),
128-
getPropertyPath(),
129-
getFetchLeftHandSideUid()
150+
(CompositeQuerySpace) join.getRightHandSide(),
151+
allowCollectionFetches,
152+
getPropertyPath()
130153
);
131154
addFetch( fetch );
132155
return fetch;
@@ -137,6 +160,15 @@ public CollectionFetch buildCollectionFetch(
137160
AssociationAttributeDefinition attributeDefinition,
138161
FetchStrategy fetchStrategy,
139162
LoadPlanBuildingContext loadPlanBuildingContext) {
163+
if ( !allowCollectionFetches ) {
164+
throw new WalkingException(
165+
String.format(
166+
"This composite path [%s] does not allow collection fetches (composite id or composite collection index/element",
167+
propertyPath.getFullPath()
168+
)
169+
);
170+
}
171+
140172
// general question here wrt Joins and collection fetches... do we create multiple Joins for many-to-many,
141173
// for example, or do we allow the Collection QuerySpace to handle that?
142174

hibernate-core/src/main/java/org/hibernate/loader/plan2/build/internal/returns/AbstractEntityReference.java

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,19 +69,17 @@ public AbstractEntityReference(
6969
PropertyPath propertyPath) {
7070
this.entityQuerySpace = entityQuerySpace;
7171
this.propertyPath = propertyPath;
72-
this.identifierDescription = buildIdentifierDescription( entityQuerySpace );
72+
this.identifierDescription = buildIdentifierDescription();
7373
}
7474

7575

7676
/**
7777
* Builds just the first level of identifier description. This will be either a simple id descriptor (String,
7878
* Long, etc) or some form of composite id (either encapsulated or not).
7979
*
80-
* @param querySpace The entity query space
81-
*
8280
* @return the descriptor for the identifier
8381
*/
84-
private EntityIdentifierDescription buildIdentifierDescription(EntityQuerySpace querySpace) {
82+
private EntityIdentifierDescription buildIdentifierDescription() {
8583
final EntityPersister persister = entityQuerySpace.getEntityPersister();
8684
final EntityIdentifierDefinition identifierDefinition = persister.getEntityKeyDefinition();
8785

@@ -166,9 +164,8 @@ public EntityFetch buildEntityFetch(
166164
)
167165
);
168166
}
169-
final ExpandingQuerySpace leftHandSide = (ExpandingQuerySpace) loadPlanBuildingContext.getQuerySpaces().getQuerySpaceByUid(
170-
getQuerySpaceUid()
171-
);
167+
168+
final ExpandingQuerySpace leftHandSide = (ExpandingQuerySpace) entityQuerySpace;
172169
final Join join = leftHandSide.addEntityJoin(
173170
attributeDefinition,
174171
fetchedPersister,
@@ -196,9 +193,17 @@ private void addFetch(Fetch fetch) {
196193
public CompositeFetch buildCompositeFetch(
197194
CompositionDefinition attributeDefinition,
198195
LoadPlanBuildingContext loadPlanBuildingContext) {
196+
final ExpandingQuerySpace leftHandSide = (ExpandingQuerySpace) entityQuerySpace;
197+
final Join join = leftHandSide.addCompositeJoin(
198+
attributeDefinition,
199+
loadPlanBuildingContext.getQuerySpaces().generateImplicitUid()
200+
);
201+
199202
final CompositeFetchImpl fetch = new CompositeFetchImpl(
200203
this,
201204
attributeDefinition.getType(),
205+
(CompositeQuerySpace) join.getRightHandSide(),
206+
true,
202207
getPropertyPath()
203208
);
204209
addFetch( fetch );

hibernate-core/src/main/java/org/hibernate/loader/plan2/build/internal/returns/CollectionFetchableElementCompositeGraph.java

Lines changed: 6 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -23,43 +23,34 @@
2323
*/
2424
package org.hibernate.loader.plan2.build.internal.returns;
2525

26-
import org.hibernate.engine.FetchStrategy;
27-
import org.hibernate.loader.plan2.build.spi.LoadPlanBuildingContext;
28-
import org.hibernate.loader.plan2.spi.CollectionFetch;
2926
import org.hibernate.loader.plan2.spi.CollectionFetchableElement;
3027
import org.hibernate.loader.plan2.spi.CollectionReference;
3128
import org.hibernate.loader.plan2.spi.CompositeFetch;
29+
import org.hibernate.loader.plan2.spi.CompositeQuerySpace;
3230
import org.hibernate.loader.plan2.spi.FetchSource;
3331
import org.hibernate.loader.plan2.spi.Join;
34-
import org.hibernate.persister.walking.spi.AssociationAttributeDefinition;
35-
import org.hibernate.persister.walking.spi.WalkingException;
3632
import org.hibernate.type.CompositeType;
3733

3834
/**
35+
* Models the element graph of a collection, where the elements are composite
36+
*
3937
* @author Steve Ebersole
4038
*/
4139
public class CollectionFetchableElementCompositeGraph
4240
extends AbstractCompositeFetch
4341
implements CompositeFetch, CollectionFetchableElement {
4442

4543
private final CollectionReference collectionReference;
46-
private final Join compositeJoin;
4744

48-
public CollectionFetchableElementCompositeGraph(
49-
CollectionReference collectionReference,
50-
Join compositeJoin) {
45+
public CollectionFetchableElementCompositeGraph(CollectionReference collectionReference, Join compositeJoin) {
5146
super(
5247
(CompositeType) compositeJoin.getRightHandSide().getPropertyMapping().getType(),
48+
(CompositeQuerySpace) compositeJoin.getRightHandSide(),
49+
false,
5350
// these property paths are just informational...
5451
collectionReference.getPropertyPath().append( "<element>" )
5552
);
5653
this.collectionReference = collectionReference;
57-
this.compositeJoin = compositeJoin;
58-
}
59-
60-
@Override
61-
protected String getFetchLeftHandSideUid() {
62-
return compositeJoin.getRightHandSide().getUid();
6354
}
6455

6556
@Override
@@ -71,17 +62,4 @@ public CollectionReference getCollectionReference() {
7162
public FetchSource getSource() {
7263
return collectionReference.getElementGraph();
7364
}
74-
75-
@Override
76-
public CollectionFetch buildCollectionFetch(
77-
AssociationAttributeDefinition attributeDefinition,
78-
FetchStrategy fetchStrategy,
79-
LoadPlanBuildingContext loadPlanBuildingContext) {
80-
throw new WalkingException( "Encountered collection as part of fetched Collection composite-element" );
81-
}
82-
83-
@Override
84-
public String getQuerySpaceUid() {
85-
return compositeJoin.getLeftHandSide().getUid();
86-
}
8765
}

hibernate-core/src/main/java/org/hibernate/loader/plan2/build/internal/returns/CollectionFetchableIndexCompositeGraph.java

Lines changed: 6 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -23,39 +23,37 @@
2323
*/
2424
package org.hibernate.loader.plan2.build.internal.returns;
2525

26-
import org.hibernate.engine.FetchStrategy;
27-
import org.hibernate.loader.plan2.build.spi.LoadPlanBuildingContext;
28-
import org.hibernate.loader.plan2.spi.CollectionFetch;
2926
import org.hibernate.loader.plan2.spi.CollectionFetchableIndex;
3027
import org.hibernate.loader.plan2.spi.CollectionReference;
3128
import org.hibernate.loader.plan2.spi.CompositeFetch;
29+
import org.hibernate.loader.plan2.spi.CompositeQuerySpace;
3230
import org.hibernate.loader.plan2.spi.FetchSource;
3331
import org.hibernate.loader.plan2.spi.Join;
34-
import org.hibernate.persister.walking.spi.AssociationAttributeDefinition;
35-
import org.hibernate.persister.walking.spi.AttributeDefinition;
36-
import org.hibernate.persister.walking.spi.WalkingException;
3732
import org.hibernate.type.CompositeType;
3833
import org.hibernate.type.Type;
3934

4035
/**
36+
* Models the index graph of a collection, where the index are composite. This can only be a Map, where the keys are
37+
* composite
38+
*
4139
* @author Steve Ebersole
4240
*/
4341
public class CollectionFetchableIndexCompositeGraph
4442
extends AbstractCompositeFetch
4543
implements CompositeFetch, CollectionFetchableIndex {
4644

4745
private final CollectionReference collectionReference;
48-
private final Join compositeJoin;
4946

5047
public CollectionFetchableIndexCompositeGraph(
5148
CollectionReference collectionReference,
5249
Join compositeJoin) {
5350
super(
5451
extractIndexType( compositeJoin ),
52+
(CompositeQuerySpace) compositeJoin.getRightHandSide(),
53+
false,
5554
collectionReference.getPropertyPath().append( "<index>" )
5655
);
5756
this.collectionReference = collectionReference;
58-
this.compositeJoin = compositeJoin;
5957
}
6058

6159
private static CompositeType extractIndexType(Join compositeJoin) {
@@ -67,12 +65,6 @@ private static CompositeType extractIndexType(Join compositeJoin) {
6765
throw new IllegalArgumentException( "Could note extract collection composite-index" );
6866
}
6967

70-
71-
@Override
72-
protected String getFetchLeftHandSideUid() {
73-
return compositeJoin.getRightHandSide().getUid();
74-
}
75-
7668
@Override
7769
public CollectionReference getCollectionReference() {
7870
return collectionReference;
@@ -82,24 +74,4 @@ public CollectionReference getCollectionReference() {
8274
public FetchSource getSource() {
8375
return collectionReference.getIndexGraph();
8476
}
85-
86-
@Override
87-
public void validateFetchPlan(FetchStrategy fetchStrategy, AttributeDefinition attributeDefinition) {
88-
// metamodel should already disallow collections to be defined as part of a collection composite-index
89-
// so, nothing to do here
90-
super.validateFetchPlan( fetchStrategy, attributeDefinition );
91-
}
92-
93-
@Override
94-
public CollectionFetch buildCollectionFetch(
95-
AssociationAttributeDefinition attributeDefinition,
96-
FetchStrategy fetchStrategy,
97-
LoadPlanBuildingContext loadPlanBuildingContext) {
98-
throw new WalkingException( "Encountered collection as part of the Map composite-index" );
99-
}
100-
101-
@Override
102-
public String getQuerySpaceUid() {
103-
return compositeJoin.getRightHandSide().getUid();
104-
}
10577
}

0 commit comments

Comments
 (0)