EagerCollectionFetch generates collection and index fetches twice

This commit is contained in:
Andrea Boriero 2020-06-25 16:40:20 +01:00
parent dca850f5ad
commit 6aefe00c1e
8 changed files with 207 additions and 4 deletions

View File

@ -21,6 +21,7 @@ import org.hibernate.metamodel.mapping.PluralAttributeMapping;
import org.hibernate.persister.collection.CollectionPersister;
import org.hibernate.query.NavigablePath;
import org.hibernate.sql.ast.tree.from.TableGroup;
import org.hibernate.sql.results.graph.Fetch;
import org.hibernate.sql.results.graph.collection.internal.BagInitializerProducer;
import org.hibernate.sql.results.graph.DomainResultCreationState;
import org.hibernate.sql.results.graph.FetchParent;
@ -95,4 +96,45 @@ public abstract class AbstractBagSemantics<B extends Collection<?>> implements C
)
);
}
@Override
public CollectionInitializerProducer createInitializerProducer(
NavigablePath navigablePath,
PluralAttributeMapping attributeMapping,
FetchParent fetchParent,
boolean selected,
String resultVariable,
LockMode lockMode,
Fetch indexFetch,
Fetch elementFetch,
DomainResultCreationState creationState){
if ( indexFetch == null ) {
indexFetch = attributeMapping.getIdentifierDescriptor() == null ? null : attributeMapping.getIdentifierDescriptor().generateFetch(
fetchParent,
navigablePath.append( CollectionPart.Nature.ID.getName() ),
FetchTiming.IMMEDIATE,
selected,
lockMode,
null,
creationState
);
}
if ( elementFetch == null ) {
elementFetch = attributeMapping.getElementDescriptor().generateFetch(
fetchParent,
navigablePath.append( CollectionPart.Nature.ELEMENT.getName() ),
FetchTiming.IMMEDIATE,
selected,
lockMode,
null,
creationState
);
}
return new BagInitializerProducer(
attributeMapping,
indexFetch,
elementFetch
);
}
}

View File

@ -19,7 +19,7 @@ import org.hibernate.engine.FetchTiming;
import org.hibernate.metamodel.mapping.CollectionPart;
import org.hibernate.metamodel.mapping.PluralAttributeMapping;
import org.hibernate.query.NavigablePath;
import org.hibernate.sql.ast.tree.from.TableGroup;
import org.hibernate.sql.results.graph.Fetch;
import org.hibernate.sql.results.graph.collection.internal.MapInitializerProducer;
import org.hibernate.sql.results.graph.DomainResultCreationState;
import org.hibernate.sql.results.graph.FetchParent;
@ -108,4 +108,45 @@ public abstract class AbstractMapSemantics<M extends Map<?,?>> implements MapSem
)
);
}
@Override
public CollectionInitializerProducer createInitializerProducer(
NavigablePath navigablePath,
PluralAttributeMapping attributeMapping,
FetchParent fetchParent,
boolean selected,
String resultVariable,
LockMode lockMode,
Fetch indexFetch,
Fetch elementFetch,
DomainResultCreationState creationState){
if ( indexFetch == null ) {
indexFetch = attributeMapping.getIndexDescriptor().generateFetch(
fetchParent,
navigablePath.append( CollectionPart.Nature.INDEX.getName() ),
FetchTiming.IMMEDIATE,
selected,
lockMode,
null,
creationState
);
}
if ( elementFetch == null ) {
elementFetch = attributeMapping.getElementDescriptor().generateFetch(
fetchParent,
navigablePath.append( CollectionPart.Nature.ELEMENT.getName() ),
FetchTiming.IMMEDIATE,
selected,
lockMode,
null,
creationState
);
}
return new MapInitializerProducer(
attributeMapping,
indexFetch,
elementFetch
);
}
}

View File

@ -17,6 +17,7 @@ import org.hibernate.engine.FetchTiming;
import org.hibernate.metamodel.mapping.CollectionPart;
import org.hibernate.metamodel.mapping.PluralAttributeMapping;
import org.hibernate.query.NavigablePath;
import org.hibernate.sql.results.graph.Fetch;
import org.hibernate.sql.results.graph.collection.internal.SetInitializerProducer;
import org.hibernate.sql.results.graph.DomainResultCreationState;
import org.hibernate.sql.results.graph.FetchParent;
@ -69,4 +70,29 @@ public abstract class AbstractSetSemantics<S extends Set<?>> implements Collecti
)
);
}
@Override
public CollectionInitializerProducer createInitializerProducer(
NavigablePath navigablePath,
PluralAttributeMapping attributeMapping,
FetchParent fetchParent,
boolean selected,
String resultVariable,
LockMode lockMode,
Fetch indexFetch,
Fetch elementFetch,
DomainResultCreationState creationState){
if ( elementFetch == null ) {
return createInitializerProducer(
navigablePath,
attributeMapping,
fetchParent,
selected,
resultVariable,
lockMode,
creationState
);
}
return new SetInitializerProducer( attributeMapping, elementFetch );
}
}

View File

@ -21,7 +21,7 @@ import org.hibernate.metamodel.mapping.CollectionPart;
import org.hibernate.metamodel.mapping.PluralAttributeMapping;
import org.hibernate.persister.collection.CollectionPersister;
import org.hibernate.query.NavigablePath;
import org.hibernate.sql.ast.tree.from.TableGroup;
import org.hibernate.sql.results.graph.Fetch;
import org.hibernate.sql.results.graph.collection.internal.ArrayInitializerProducer;
import org.hibernate.sql.results.graph.DomainResultCreationState;
import org.hibernate.sql.results.graph.FetchParent;
@ -126,4 +126,45 @@ public class StandardArraySemantics implements CollectionSemantics<Object[]> {
)
);
}
@Override
public CollectionInitializerProducer createInitializerProducer(
NavigablePath navigablePath,
PluralAttributeMapping attributeMapping,
FetchParent fetchParent,
boolean selected,
String resultVariable,
LockMode lockMode,
Fetch indexFetch,
Fetch elementFetch,
DomainResultCreationState creationState){
if ( indexFetch == null ) {
indexFetch = attributeMapping.getIndexDescriptor().generateFetch(
fetchParent,
navigablePath.append( CollectionPart.Nature.INDEX.getName() ),
FetchTiming.IMMEDIATE,
selected,
lockMode,
null,
creationState
);
}
if ( elementFetch == null ) {
elementFetch = attributeMapping.getElementDescriptor().generateFetch(
fetchParent,
navigablePath.append( CollectionPart.Nature.ELEMENT.getName() ),
FetchTiming.IMMEDIATE,
selected,
lockMode,
null,
creationState
);
}
return new ArrayInitializerProducer(
attributeMapping,
indexFetch,
elementFetch
);
}
}

View File

@ -22,7 +22,7 @@ import org.hibernate.metamodel.mapping.CollectionPart;
import org.hibernate.metamodel.mapping.PluralAttributeMapping;
import org.hibernate.persister.collection.CollectionPersister;
import org.hibernate.query.NavigablePath;
import org.hibernate.sql.ast.tree.from.TableGroup;
import org.hibernate.sql.results.graph.Fetch;
import org.hibernate.sql.results.graph.collection.internal.ListInitializerProducer;
import org.hibernate.sql.results.graph.DomainResultCreationState;
import org.hibernate.sql.results.graph.FetchParent;
@ -101,6 +101,46 @@ public class StandardListSemantics implements CollectionSemantics<List> {
);
}
@Override
public CollectionInitializerProducer createInitializerProducer(
NavigablePath navigablePath,
PluralAttributeMapping attributeMapping,
FetchParent fetchParent,
boolean selected,
String resultVariable,
LockMode lockMode,
Fetch indexFetch,
Fetch elementFetch,
DomainResultCreationState creationState) {
if ( indexFetch == null ) {
indexFetch = attributeMapping.getIndexDescriptor().generateFetch(
fetchParent,
navigablePath.append( CollectionPart.Nature.INDEX.getName() ),
FetchTiming.IMMEDIATE,
selected,
lockMode,
null,
creationState
);
}
if ( elementFetch == null ) {
elementFetch = attributeMapping.getElementDescriptor().generateFetch(
fetchParent,
navigablePath.append( CollectionPart.Nature.ELEMENT.getName() ),
FetchTiming.IMMEDIATE,
selected,
lockMode,
null,
creationState
);
}
return new ListInitializerProducer(
attributeMapping,
indexFetch,
elementFetch
);
}
@Override
public PersistentCollection instantiateWrapper(
Object key,

View File

@ -17,6 +17,7 @@ import org.hibernate.metamodel.mapping.PluralAttributeMapping;
import org.hibernate.persister.collection.CollectionPersister;
import org.hibernate.query.NavigablePath;
import org.hibernate.sql.results.graph.DomainResultCreationState;
import org.hibernate.sql.results.graph.Fetch;
import org.hibernate.sql.results.graph.FetchParent;
/**
@ -66,4 +67,15 @@ public interface CollectionSemantics<C> {
String resultVariable,
LockMode lockMode,
DomainResultCreationState creationState);
CollectionInitializerProducer createInitializerProducer(
NavigablePath navigablePath,
PluralAttributeMapping attributeMapping,
FetchParent fetchParent,
boolean selected,
String resultVariable,
LockMode lockMode,
Fetch indexFetch,
Fetch elementFetch,
DomainResultCreationState creationState);
}

View File

@ -12,7 +12,6 @@ import org.hibernate.engine.FetchStyle;
import org.hibernate.engine.FetchTiming;
import org.hibernate.mapping.Collection;
import org.hibernate.mapping.Value;
import org.hibernate.metamodel.mapping.Association;
import org.hibernate.metamodel.mapping.CollectionPart;
import org.hibernate.metamodel.mapping.EntityAssociationMapping;
import org.hibernate.metamodel.mapping.EntityMappingType;

View File

@ -98,6 +98,8 @@ public class EagerCollectionFetch extends CollectionFetch implements FetchParent
null,
// todo (6.0) : we need to propagate these lock modes
LockMode.READ,
indexFetch,
elementFetch,
creationState
);
}