Cleanup some model part APIs

This commit is contained in:
Christian Beikov 2021-12-02 11:34:12 +01:00
parent 2977860fc9
commit 4cdc1c24fa
9 changed files with 214 additions and 140 deletions

View File

@ -7,12 +7,19 @@
package org.hibernate.metamodel.mapping;
import java.util.List;
import java.util.function.BiConsumer;
import org.hibernate.NotYetImplementedFor6Exception;
import org.hibernate.mapping.IndexedConsumer;
import org.hibernate.metamodel.mapping.internal.EmbeddedAttributeMapping;
import org.hibernate.metamodel.mapping.internal.MappingModelCreationProcess;
import org.hibernate.metamodel.spi.EmbeddableRepresentationStrategy;
import org.hibernate.query.NavigablePath;
import org.hibernate.sql.ast.spi.SqlSelection;
import org.hibernate.sql.ast.tree.from.TableGroup;
import org.hibernate.sql.ast.tree.from.TableGroupProducer;
import org.hibernate.sql.results.graph.DomainResult;
import org.hibernate.sql.results.graph.DomainResultCreationState;
/**
* Describes an embeddable - the actual type
@ -48,4 +55,39 @@ public interface EmbeddableMappingType extends ManagedMappingType, SelectableMap
@Override
int forEachJdbcType(int offset, IndexedConsumer<JdbcMapping> action);
// Make this abstract again to ensure subclasses implement this method
@Override
<T> DomainResult<T> createDomainResult(
NavigablePath navigablePath,
TableGroup tableGroup,
String resultVariable,
DomainResultCreationState creationState);
@Override
default void applySqlSelections(
NavigablePath navigablePath,
TableGroup tableGroup,
DomainResultCreationState creationState) {
visitAttributeMappings(
attributeMapping -> attributeMapping.applySqlSelections( navigablePath, tableGroup, creationState )
);
}
@Override
default void applySqlSelections(
NavigablePath navigablePath,
TableGroup tableGroup,
DomainResultCreationState creationState,
BiConsumer<SqlSelection, JdbcMapping> selectionConsumer) {
visitAttributeMappings(
attributeMapping ->
attributeMapping.applySqlSelections(
navigablePath,
tableGroup,
creationState,
selectionConsumer
)
);
}
}

View File

@ -10,29 +10,36 @@ import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Supplier;
import org.hibernate.NotYetImplementedFor6Exception;
import org.hibernate.engine.spi.LoadQueryInfluencers;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.loader.ast.spi.Loadable;
import org.hibernate.loader.ast.spi.MultiNaturalIdLoader;
import org.hibernate.loader.ast.spi.NaturalIdLoader;
import org.hibernate.mapping.IndexedConsumer;
import org.hibernate.metamodel.UnsupportedMappingException;
import org.hibernate.metamodel.spi.EntityRepresentationStrategy;
import org.hibernate.persister.entity.EntityPersister;
import org.hibernate.query.NavigablePath;
import org.hibernate.query.sqm.mutation.spi.SqmMultiTableMutationStrategy;
import org.hibernate.sql.ast.Clause;
import org.hibernate.sql.ast.spi.FromClauseAccess;
import org.hibernate.sql.ast.spi.SqlAliasBase;
import org.hibernate.sql.ast.spi.SqlAstCreationContext;
import org.hibernate.sql.ast.spi.SqlAstCreationState;
import org.hibernate.sql.ast.spi.SqlExpressionResolver;
import org.hibernate.sql.ast.spi.SqlSelection;
import org.hibernate.sql.ast.tree.from.TableGroup;
import org.hibernate.sql.ast.tree.from.TableReference;
import org.hibernate.sql.ast.tree.from.TableReferenceJoin;
import org.hibernate.sql.ast.tree.predicate.Predicate;
import org.hibernate.sql.results.graph.DomainResult;
import org.hibernate.sql.results.graph.DomainResultAssembler;
import org.hibernate.sql.results.graph.DomainResultCreationState;
import org.hibernate.sql.results.jdbc.spi.RowProcessingState;
import org.hibernate.type.descriptor.java.JavaType;
@ -92,6 +99,85 @@ public interface EntityMappingType extends ManagedMappingType, EntityValuedModel
void visitQuerySpaces(Consumer<String> querySpaceConsumer);
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Make sure we don't run into possible stack overflows
@Override
default ModelPart findSubPart(String name) {
return findSubPart( name, null );
}
@Override
default ModelPart findSubPart(String name, EntityMappingType targetType) {
throw new NotYetImplementedFor6Exception( getClass() );
}
@Override
default void visitSubParts(Consumer<ModelPart> consumer, EntityMappingType targetType) {
throw new NotYetImplementedFor6Exception( getClass() );
}
@Override
default <T> DomainResult<T> createDomainResult(
NavigablePath navigablePath,
TableGroup tableGroup,
String resultVariable,
DomainResultCreationState creationState) {
throw new NotYetImplementedFor6Exception( getClass() );
}
@Override
default void applySqlSelections(
NavigablePath navigablePath,
TableGroup tableGroup,
DomainResultCreationState creationState) {
throw new NotYetImplementedFor6Exception( getClass() );
}
@Override
default void applySqlSelections(
NavigablePath navigablePath,
TableGroup tableGroup,
DomainResultCreationState creationState,
BiConsumer<SqlSelection,JdbcMapping> selectionConsumer) {
throw new NotYetImplementedFor6Exception( getClass() );
}
@Override
default int getJdbcTypeCount() {
return forEachJdbcType( (index, jdbcMapping) -> {} );
}
@Override
default int forEachJdbcType(int offset, IndexedConsumer<JdbcMapping> action) {
throw new NotYetImplementedFor6Exception( getClass() );
}
@Override
default Object disassemble(Object value, SharedSessionContractImplementor session) {
throw new NotYetImplementedFor6Exception( getClass() );
}
@Override
default int forEachDisassembledJdbcValue(
Object value,
Clause clause,
int offset,
JdbcValuesConsumer valuesConsumer,
SharedSessionContractImplementor session) {
throw new NotYetImplementedFor6Exception( getClass() );
}
@Override
default int forEachJdbcValue(
Object value,
Clause clause,
int offset,
JdbcValuesConsumer consumer,
SharedSessionContractImplementor session) {
return forEachDisassembledJdbcValue( disassemble( value, session ), clause, offset, consumer, session );
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Inheritance

View File

@ -74,32 +74,17 @@ public interface EntityValuedModelPart extends FetchableContainer {
@Override
default int getJdbcTypeCount() {
int span = 0;
final List<AttributeMapping> attributeMappings = getEntityMappingType().getAttributeMappings();
for ( int i = 0; i < attributeMappings.size(); i++ ) {
span += attributeMappings.get( i ).getJdbcTypeCount();
}
return span;
return getEntityMappingType().getJdbcTypeCount();
}
@Override
default int forEachJdbcType(int offset, IndexedConsumer<JdbcMapping> action) {
int span = 0;
final List<AttributeMapping> attributeMappings = getEntityMappingType().getAttributeMappings();
for ( int i = 0; i < attributeMappings.size(); i++ ) {
span += attributeMappings.get( i ).forEachJdbcType( span + offset, action );
}
return span;
return getEntityMappingType().forEachJdbcType( offset, action );
}
@Override
default Object disassemble(Object value, SharedSessionContractImplementor session) {
if ( value == null ) {
return null;
}
final EntityIdentifierMapping identifierMapping = getEntityMappingType().getIdentifierMapping();
final Object identifier = identifierMapping.getIdentifier( value, session );
return identifierMapping.disassemble( identifier, session );
return getEntityMappingType().disassemble( value, session );
}
@Override
@ -119,12 +104,6 @@ public interface EntityValuedModelPart extends FetchableContainer {
int offset,
JdbcValuesConsumer consumer,
SharedSessionContractImplementor session) {
int span = 0;
final List<AttributeMapping> attributeMappings = getEntityMappingType().getAttributeMappings();
for ( int i = 0; i < attributeMappings.size(); i++ ) {
final AttributeMapping attributeMapping = attributeMappings.get( i );
span += attributeMapping.forEachJdbcValue( value, clause, span + offset, consumer, session );
}
return span;
return getEntityMappingType().forEachJdbcValue( value, clause, offset, consumer, session );
}
}

View File

@ -326,17 +326,7 @@ public class CompoundNaturalIdMapping extends AbstractNaturalIdMapping implement
assert values.length == attributes.size();
for ( int i = 0; i < attributes.size(); i++ ) {
final SingularAttributeMapping attributeMapping = attributes.get( i );
final Object value = values[ i ];
if ( attributeMapping instanceof ToOneAttributeMapping ) {
final ToOneAttributeMapping toOne = (ToOneAttributeMapping) attributeMapping;
final ForeignKeyDescriptor fKDescriptor = toOne.getForeignKeyDescriptor();
final Object keyValue = value == null ? null : fKDescriptor.disassemble( value, session );
fKDescriptor.breakDownJdbcValues( keyValue, valueConsumer, session );
}
else {
attributeMapping.breakDownJdbcValues( value, valueConsumer, session );
}
attributes.get( i ).breakDownJdbcValues( values[ i ], valueConsumer, session );
}
}

View File

@ -622,33 +622,6 @@ public class EmbeddableMappingTypeImpl extends AbstractEmbeddableMapping impleme
);
}
@Override
public void applySqlSelections(
NavigablePath navigablePath,
TableGroup tableGroup,
DomainResultCreationState creationState) {
visitAttributeMappings(
attributeMapping -> attributeMapping.applySqlSelections( navigablePath, tableGroup, creationState )
);
}
@Override
public void applySqlSelections(
NavigablePath navigablePath,
TableGroup tableGroup,
DomainResultCreationState creationState,
BiConsumer<SqlSelection, JdbcMapping> selectionConsumer) {
visitAttributeMappings(
attributeMapping ->
attributeMapping.applySqlSelections(
navigablePath,
tableGroup,
creationState,
selectionConsumer
)
);
}
@Override
public int getNumberOfFetchables() {
return attributeMappings.size();

View File

@ -142,18 +142,7 @@ public class NonAggregatedIdentifierMappingImpl extends AbstractCompositeIdentif
@Override
public Object disassemble(Object value, SharedSessionContractImplementor session) {
if ( hasContainingClass() ) {
final Object[] result = new Object[ identifierValueMapper.getAttributeMappings().size()];
for ( int i = 0; i < identifierValueMapper.getAttributeMappings().size(); i++ ) {
final AttributeMapping attributeMapping = identifierValueMapper.getAttributeMappings().get( i );
Object o = attributeMapping.getPropertyAccess().getGetter().get( value );
result[i] = attributeMapping.disassemble( o, session );
}
return result;
}
return getEmbeddableTypeDescriptor().disassemble( value, session );
return identifierValueMapper.disassemble( value, session );
}
@Override
@ -163,34 +152,7 @@ public class NonAggregatedIdentifierMappingImpl extends AbstractCompositeIdentif
int offset,
JdbcValuesConsumer valuesConsumer,
SharedSessionContractImplementor session) {
if ( hasContainingClass() ) {
int span = 0;
for ( int i = 0; i < identifierValueMapper.getAttributeMappings().size(); i++ ) {
final AttributeMapping attributeMapping = identifierValueMapper.getAttributeMappings().get( i );
final Object o = attributeMapping.getPropertyAccess().getGetter().get( value );
if ( attributeMapping instanceof ToOneAttributeMapping ) {
final ToOneAttributeMapping toOneAttributeMapping = (ToOneAttributeMapping) attributeMapping;
final ForeignKeyDescriptor fkDescriptor = toOneAttributeMapping.getForeignKeyDescriptor();
final Object identifier = fkDescriptor.getAssociationKeyFromSide(
o,
toOneAttributeMapping.getSideNature().inverse(),
session
);
span += fkDescriptor.forEachJdbcValue(
identifier,
clause,
span + offset,
valuesConsumer,
session
);
}
else {
span += attributeMapping.forEachJdbcValue( o, clause, span + offset, valuesConsumer, session );
}
}
return span;
}
return super.forEachJdbcValue( value, clause, offset, valuesConsumer, session );
return identifierValueMapper.forEachJdbcValue( value, clause, offset, valuesConsumer, session );
}
@Override
@ -331,14 +293,7 @@ public class NonAggregatedIdentifierMappingImpl extends AbstractCompositeIdentif
@Override
public void breakDownJdbcValues(Object domainValue, JdbcValueConsumer valueConsumer, SharedSessionContractImplementor session) {
assert domainValue instanceof Object[];
final Object[] values = (Object[]) domainValue;
assert values.length == identifierValueMapper.getAttributeMappings().size();
for ( int i = 0; i < identifierValueMapper.getAttributeMappings().size(); i++ ) {
final AttributeMapping attribute = identifierValueMapper.getAttributeMappings().get( i );
attribute.breakDownJdbcValues( values[ i ], valueConsumer, session );
}
identifierValueMapper.breakDownJdbcValues( domainValue, valueConsumer, session );
}
@Override
@ -346,9 +301,7 @@ public class NonAggregatedIdentifierMappingImpl extends AbstractCompositeIdentif
NavigablePath navigablePath,
TableGroup tableGroup,
DomainResultCreationState creationState) {
for ( int i = 0; i < identifierValueMapper.getAttributeMappings().size(); i++ ) {
identifierValueMapper.getAttributeMappings().get( i ).applySqlSelections( navigablePath, tableGroup, creationState );
}
identifierValueMapper.applySqlSelections( navigablePath, tableGroup, creationState );
}
@Override
@ -357,14 +310,7 @@ public class NonAggregatedIdentifierMappingImpl extends AbstractCompositeIdentif
TableGroup tableGroup,
DomainResultCreationState creationState,
BiConsumer<SqlSelection, JdbcMapping> selectionConsumer) {
for ( int i = 0; i < identifierValueMapper.getAttributeMappings().size(); i++ ) {
identifierValueMapper.getAttributeMappings().get( i ).applySqlSelections(
navigablePath,
tableGroup,
creationState,
selectionConsumer
);
}
identifierValueMapper.applySqlSelections( navigablePath, tableGroup, creationState, selectionConsumer );
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

View File

@ -328,13 +328,7 @@ public class SimpleForeignKeyDescriptor implements ForeignKeyDescriptor, BasicVa
@Override
public Object disassemble(Object value, SharedSessionContractImplementor session) {
if ( value == null ) {
return null;
}
if ( refersToPrimaryKey && value instanceof HibernateProxy ) {
return ( (HibernateProxy) value ).getHibernateLazyInitializer().getIdentifier();
}
return ( (PropertyBasedMapping) targetSide.getModelPart() ).getPropertyAccess().getGetter().get( value );
return value;
}
@Override

View File

@ -1527,7 +1527,11 @@ public class ToOneAttributeMapping
Object domainValue,
JdbcValueConsumer valueConsumer,
SharedSessionContractImplementor session) {
foreignKeyDescriptor.breakDownJdbcValues( domainValue, valueConsumer, session );
foreignKeyDescriptor.breakDownJdbcValues(
foreignKeyDescriptor.getAssociationKeyFromSide( domainValue, sideNature.inverse(), session ),
valueConsumer,
session
);
}
@Override
@ -1578,18 +1582,34 @@ public class ToOneAttributeMapping
@Override
public Object disassemble(Object value, SharedSessionContractImplementor session) {
return foreignKeyDescriptor.disassemble( value, session );
return foreignKeyDescriptor.disassemble(
foreignKeyDescriptor.getAssociationKeyFromSide( value, sideNature.inverse(), session ),
session
);
}
@Override
public int forEachDisassembledJdbcValue(Object value, Clause clause, int offset, JdbcValuesConsumer valuesConsumer, SharedSessionContractImplementor session) {
public int forEachDisassembledJdbcValue(
Object value,
Clause clause,
int offset,
JdbcValuesConsumer valuesConsumer,
SharedSessionContractImplementor session) {
return foreignKeyDescriptor.forEachDisassembledJdbcValue( value, clause, offset, valuesConsumer, session );
}
@Override
public int forEachJdbcValue(Object value, Clause clause, int offset, JdbcValuesConsumer consumer, SharedSessionContractImplementor session) {
public int forEachJdbcValue(
Object value,
Clause clause,
int offset,
JdbcValuesConsumer consumer,
SharedSessionContractImplementor session) {
return foreignKeyDescriptor.forEachDisassembledJdbcValue(
foreignKeyDescriptor.disassemble( value, session ),
foreignKeyDescriptor.disassemble(
foreignKeyDescriptor.getAssociationKeyFromSide( value, sideNature.inverse(), session ),
session
),
clause,
offset,
consumer,

View File

@ -6429,6 +6429,42 @@ public abstract class AbstractEntityPersister
collectAttributeDefinitions();
}
@Override
public int getJdbcTypeCount() {
return getIdentifierMapping().getJdbcTypeCount();
}
@Override
public int forEachJdbcType(int offset, IndexedConsumer<JdbcMapping> action) {
return getIdentifierMapping().forEachJdbcType( offset, action );
}
@Override
public Object disassemble(Object value, SharedSessionContractImplementor session) {
if ( value == null ) {
return null;
}
final EntityIdentifierMapping identifierMapping = getIdentifierMapping();
final Object identifier = identifierMapping.getIdentifier( value, session );
return identifierMapping.disassemble( identifier, session );
}
@Override
public int forEachDisassembledJdbcValue(
Object value,
Clause clause,
int offset,
JdbcValuesConsumer valuesConsumer,
SharedSessionContractImplementor session) {
return getIdentifierMapping().forEachDisassembledJdbcValue(
value,
clause,
offset,
valuesConsumer,
session
);
}
@Override
public int forEachJdbcValue(
Object value,
@ -6436,13 +6472,21 @@ public abstract class AbstractEntityPersister
int offset,
JdbcValuesConsumer consumer,
SharedSessionContractImplementor session) {
int span = 0;
final List<AttributeMapping> mappings = getAttributeMappings();
for ( int i = 0; i < mappings.size(); i++ ) {
final AttributeMapping attributeMapping = mappings.get( i );
span += attributeMapping.forEachJdbcValue( value, clause, span + offset, consumer, session );
final EntityIdentifierMapping identifierMapping = getIdentifierMapping();
final Object identifier;
if ( value == null ) {
identifier = null;
}
return span;
else {
identifier = identifierMapping.disassemble( identifierMapping.getIdentifier( value, session ), session );
}
return identifierMapping.forEachDisassembledJdbcValue(
identifier,
clause,
offset,
consumer,
session
);
}
@Override