Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
*/
package org.hibernate.metamodel.internal;

import java.util.Map;
import java.util.function.Supplier;

import org.hibernate.mapping.Component;
Expand All @@ -30,7 +29,7 @@ public EmbeddableInstantiatorDynamicMap(

@Override
public Object instantiate(ValueAccess valuesAccess) {
final Map<?,?> dataMap = generateDataMap();
final var dataMap = generateDataMap();
final var values = valuesAccess == null ? null : valuesAccess.getValues();
if ( values != null ) {
final var mappingType = runtimeDescriptorAccess.get();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
/**
* Support for instantiating embeddables as POJO representation through a constructor
*/
public class EmbeddableInstantiatorPojoIndirecting extends AbstractPojoInstantiator implements EmbeddableInstantiator {
public class EmbeddableInstantiatorPojoIndirecting
extends AbstractPojoInstantiator
implements EmbeddableInstantiator {
protected final Constructor<?> constructor;
protected final int[] index;

Expand All @@ -30,7 +32,7 @@ public static EmbeddableInstantiatorPojoIndirecting of(
if ( componentNames == null ) {
throw new IllegalArgumentException( "Can't determine field assignment for constructor: " + constructor );
}
final int[] index = new int[componentNames.length];
final var index = new int[componentNames.length];
return EmbeddableHelper.resolveIndex( propertyNames, componentNames, index )
? new EmbeddableInstantiatorPojoIndirectingWithGap( constructor, index )
: new EmbeddableInstantiatorPojoIndirecting( constructor, index );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
* Support for instantiating embeddables as POJO representation
* using bytecode optimizer
*/
public class EmbeddableInstantiatorPojoOptimized extends AbstractPojoInstantiator implements StandardEmbeddableInstantiator {
public class EmbeddableInstantiatorPojoOptimized
extends AbstractPojoInstantiator
implements StandardEmbeddableInstantiator {
private final Supplier<EmbeddableMappingType> embeddableMappingAccess;
private final InstantiationOptimizer instantiationOptimizer;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,16 @@
/**
* Support for instantiating embeddables as POJO representation
*/
public class EmbeddableInstantiatorPojoStandard extends AbstractPojoInstantiator implements StandardEmbeddableInstantiator {
public class EmbeddableInstantiatorPojoStandard
extends AbstractPojoInstantiator
implements StandardEmbeddableInstantiator {

private final Supplier<EmbeddableMappingType> embeddableMappingAccess;
private final Constructor<?> constructor;

public EmbeddableInstantiatorPojoStandard(Class<?> embeddableClass, Supplier<EmbeddableMappingType> embeddableMappingAccess) {
public EmbeddableInstantiatorPojoStandard(
Class<?> embeddableClass,
Supplier<EmbeddableMappingType> embeddableMappingAccess) {
super( embeddableClass );
this.embeddableMappingAccess = embeddableMappingAccess;
this.constructor = resolveConstructor( embeddableClass );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ public EmbeddableInstantiatorRecordIndirecting(Class<?> javaType, int[] index) {
}

public static EmbeddableInstantiatorRecordIndirecting of(Class<?> javaType, String[] propertyNames) {
final String[] componentNames = getRecordComponentNames( javaType );
final int[] index = new int[componentNames.length];
final var componentNames = getRecordComponentNames( javaType );
final var index = new int[componentNames.length];
return EmbeddableHelper.resolveIndex( propertyNames, componentNames, index )
? new EmbeddableInstantiatorRecordIndirectingWithGap( javaType, index )
: new EmbeddableInstantiatorRecordIndirecting( javaType, index );
Expand All @@ -49,7 +49,8 @@ public Object instantiate(ValueAccess valuesAccess) {
}

// Handles gaps, by leaving the value null for that index
private static class EmbeddableInstantiatorRecordIndirectingWithGap extends EmbeddableInstantiatorRecordIndirecting {
private static class EmbeddableInstantiatorRecordIndirectingWithGap
extends EmbeddableInstantiatorRecordIndirecting {

public EmbeddableInstantiatorRecordIndirectingWithGap(Class<?> javaType, int[] index) {
super( javaType, index );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ public class EmbeddableRepresentationStrategyPojo implements EmbeddableRepresent
private final PropertyAccess[] propertyAccesses;
private final Map<String, Integer> attributeNameToPositionMap;

private final StrategySelector strategySelector;
private final ReflectionOptimizer reflectionOptimizer;
private final EmbeddableInstantiator instantiator;
private final Map<Object, EmbeddableInstantiator> instantiatorsByDiscriminator;
Expand All @@ -57,32 +56,33 @@ public EmbeddableRepresentationStrategyPojo(
propertyAccesses = new PropertyAccess[propertySpan];
attributeNameToPositionMap = new HashMap<>( propertySpan );

final var strategySelector =
creationContext.getServiceRegistry()
.getService( StrategySelector.class );

// We need access to the Class objects, used only during initialization
final var subclassesByName = getSubclassesByName( bootDescriptor, creationContext );
boolean foundCustomAccessor = false;
for ( int i = 0; i < bootDescriptor.getProperties().size(); i++ ) {
final var property = bootDescriptor.getProperty( i );
final Class<?> embeddableClass;
if ( subclassesByName != null ) {
final var subclass = subclassesByName.get( bootDescriptor.getPropertyDeclaringClass( property ) );
embeddableClass = subclass != null ? subclass : getEmbeddableJavaType().getJavaTypeClass();
}
else {
embeddableClass = getEmbeddableJavaType().getJavaTypeClass();
}
propertyAccesses[i] = buildPropertyAccess( property, embeddableClass, customInstantiator == null );
final var embeddableClass = getEmbeddableClass( bootDescriptor, subclassesByName, property );
propertyAccesses[i] =
buildPropertyAccess(
property,
embeddableClass,
customInstantiator == null,
strategySelector
);
attributeNameToPositionMap.put( property.getName(), i );

if ( !property.isBasicPropertyAccessor() ) {
foundCustomAccessor = true;
}
}

boolean hasCustomAccessors = foundCustomAccessor;
strategySelector = creationContext.getServiceRegistry().getService( StrategySelector.class );
reflectionOptimizer = buildReflectionOptimizer(
bootDescriptor,
hasCustomAccessors,
foundCustomAccessor,
propertyAccesses,
creationContext
);
Expand Down Expand Up @@ -120,6 +120,19 @@ public EmbeddableRepresentationStrategyPojo(
}
}

private Class<?> getEmbeddableClass(
Component bootDescriptor,
Map<String, Class<?>> subclassesByName,
Property property) {
if ( subclassesByName != null ) {
final var subclass = subclassesByName.get( bootDescriptor.getPropertyDeclaringClass( property ) );
return subclass != null ? subclass : getEmbeddableJavaType().getJavaTypeClass();
}
else {
return getEmbeddableJavaType().getJavaTypeClass();
}
}

private static <T> JavaType<?> resolveEmbeddableJavaType(
Component bootDescriptor,
CompositeUserType<T> compositeUserType,
Expand Down Expand Up @@ -165,7 +178,8 @@ private static ProxyFactoryFactory getProxyFactoryFactory(RuntimeModelCreationCo
private PropertyAccess buildPropertyAccess(
Property property,
Class<?> embeddableClass,
boolean requireSetters) {
boolean requireSetters,
StrategySelector strategySelector) {
final var strategy = propertyAccessStrategy( property, embeddableClass, strategySelector );
if ( strategy == null ) {
throw new HibernateException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@ public class EntityRepresentationStrategyPojoStandard implements EntityRepresent
private final ProxyFactory proxyFactory;
private final EntityInstantiator instantiator;

private final StrategySelector strategySelector;

private final String identifierPropertyName;
private final PropertyAccess identifierPropertyAccess;
private final Map<String, PropertyAccess> propertyAccessMap;
Expand All @@ -73,17 +71,23 @@ public EntityRepresentationStrategyPojoStandard(

isBytecodeEnhanced = isPersistentAttributeInterceptableType( mappedJavaType );

final var strategySelector =
creationContext.getServiceRegistry()
.getService( StrategySelector.class );

final var identifierProperty = bootDescriptor.getIdentifierProperty();
if ( identifierProperty == null ) {
identifierPropertyName = null;
identifierPropertyAccess = null;

if ( bootDescriptor.getIdentifier() instanceof Component descriptorIdentifierComponent ) {
final Component identifierMapper = bootDescriptor.getIdentifierMapper();
final var identifierMapper = bootDescriptor.getIdentifierMapper();
mapsIdRepresentationStrategy = new EmbeddableRepresentationStrategyPojo(
identifierMapper == null ? descriptorIdentifierComponent : identifierMapper,
() -> {
final var type = (CompositeTypeImplementor) bootDescriptor.getIdentifierMapper().getType();
final var type =
(CompositeTypeImplementor)
bootDescriptor.getIdentifierMapper().getType();
return type.getMappingModelPart().getEmbeddableTypeDescriptor();
},
// we currently do not support custom instantiators for identifiers
Expand All @@ -99,11 +103,9 @@ public EntityRepresentationStrategyPojoStandard(
else {
mapsIdRepresentationStrategy = null;
identifierPropertyName = identifierProperty.getName();
identifierPropertyAccess = makePropertyAccess( identifierProperty );
identifierPropertyAccess = makePropertyAccess( identifierProperty, strategySelector );
}

strategySelector = creationContext.getServiceRegistry().getService( StrategySelector.class );

final var bytecodeProvider =
creationContext.getBootstrapContext().getServiceRegistry()
.requireService( BytecodeProvider.class );
Expand All @@ -116,7 +118,7 @@ public EntityRepresentationStrategyPojoStandard(
creationContext
);

propertyAccessMap = buildPropertyAccessMap( bootDescriptor );
propertyAccessMap = buildPropertyAccessMap( bootDescriptor, strategySelector );
reflectionOptimizer = resolveReflectionOptimizer( bytecodeProvider );

instantiator = determineInstantiator( bootDescriptor, runtimeDescriptor );
Expand All @@ -129,7 +131,8 @@ private ProxyFactory resolveProxyFactory(
BytecodeProvider bytecodeProvider,
RuntimeModelCreationContext creationContext) {
if ( entityPersister.isAbstract() && bootDescriptor.isConcreteProxy() ) {
// The entity class is abstract, but the hierarchy always gets entities loaded/proxied using their concrete type.
// The entity class is abstract, but the hierarchy always
// gets entities loaded/proxied using their concrete type.
// So we do not need proxies for this entity class.
return null;
}
Expand All @@ -143,9 +146,10 @@ else if ( entityPersister.getBytecodeEnhancementMetadata().isEnhancedForLazyLoad
}
else {
if ( proxyJavaType != null && entityPersister.isLazy() ) {
final var proxyFactory = createProxyFactory( bootDescriptor, bytecodeProvider, creationContext );
final var proxyFactory =
createProxyFactory( bootDescriptor, bytecodeProvider, creationContext );
if ( proxyFactory == null ) {
((EntityMetamodel) entityPersister).setLazy( false );
( (EntityMetamodel) entityPersister ).setLazy( false );
}
return proxyFactory;
}
Expand All @@ -155,10 +159,11 @@ else if ( entityPersister.getBytecodeEnhancementMetadata().isEnhancedForLazyLoad
}
}

private Map<String, PropertyAccess> buildPropertyAccessMap(PersistentClass bootDescriptor) {
private Map<String, PropertyAccess> buildPropertyAccessMap(
PersistentClass bootDescriptor, StrategySelector strategySelector) {
final Map<String, PropertyAccess> propertyAccessMap = new LinkedHashMap<>();
for ( var property : bootDescriptor.getAllPropertyClosure() ) {
propertyAccessMap.put( property.getName(), makePropertyAccess( property ) );
propertyAccessMap.put( property.getName(), makePropertyAccess( property, strategySelector ) );
}
return propertyAccessMap;
}
Expand Down Expand Up @@ -309,7 +314,7 @@ private ReflectionOptimizer resolveReflectionOptimizer(BytecodeProvider bytecode
return bytecodeProvider.getReflectionOptimizer( mappedJtd.getJavaTypeClass(), propertyAccessMap );
}

private PropertyAccess makePropertyAccess(Property bootAttributeDescriptor) {
private PropertyAccess makePropertyAccess(Property bootAttributeDescriptor, StrategySelector strategySelector) {
final var mappedClass = mappedJtd.getJavaTypeClass();
final String descriptorName = bootAttributeDescriptor.getName();
final var strategy = propertyAccessStrategy( bootAttributeDescriptor, mappedClass, strategySelector );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,6 @@ public BasicFetch<?> generateFetch(
discriminatorType.getValueConverter(),
fetchTiming,
true,
creationState,
false,
!sqlSelection.isVirtual()
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import org.hibernate.metamodel.mapping.ModelPart;
import org.hibernate.metamodel.mapping.SelectableMapping;
import org.hibernate.metamodel.mapping.ordering.ast.DomainPath;
import org.hibernate.metamodel.mapping.ordering.ast.OrderingExpression;
import org.hibernate.query.SortDirection;
import org.hibernate.sql.ast.spi.SqlAstCreationState;
import org.hibernate.sql.ast.tree.SqlAstNode;
Expand All @@ -28,6 +27,7 @@
import org.hibernate.sql.results.internal.SqlSelectionImpl;

import static org.hibernate.internal.util.NullnessUtil.castNonNull;
import static org.hibernate.metamodel.mapping.ordering.ast.OrderingExpression.applyCollation;
import static org.hibernate.sql.ast.spi.SqlExpressionResolver.createColumnReferenceKey;

/**
Expand Down Expand Up @@ -209,9 +209,11 @@ private void addSortSpecification(
);
}
else {
final var subPart = embeddableValuedModelPart.findSubPart( modelPartName, null );
final var subPart =
embeddableValuedModelPart.findSubPart( modelPartName, null )
.asBasicValuedModelPart();
addSortSpecification(
castNonNull( subPart.asBasicValuedModelPart() ),
castNonNull( subPart ),
ast,
tableGroup,
collation,
Expand Down Expand Up @@ -260,8 +262,7 @@ && selectClauseDoesNotContainOrderExpression( expression, selectClause ) ) {
selectClause.addSqlSelection( new SqlSelectionImpl( valuesArrayPosition, expression ) );
}

final var sortExpression =
OrderingExpression.applyCollation( expression, collation, creationState );
final var sortExpression = applyCollation( expression, collation, creationState );
ast.addSortSpecification( new SortSpecification( sortExpression, sortOrder, nullPrecedence ) );
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@
import org.hibernate.type.descriptor.java.JavaType;
import org.hibernate.type.descriptor.java.MutabilityPlan;

import static org.hibernate.metamodel.mapping.internal.MappingModelCreationHelper.buildBasicAttributeMapping;

/**
* Base support for EmbeddableMappingType implementations
*/
Expand Down Expand Up @@ -351,7 +353,7 @@ protected boolean finishInitialization(
selectablePath = new SelectablePath( determineEmbeddablePrefix() + bootPropertyDescriptor.getName() );
}

attributeMapping = MappingModelCreationHelper.buildBasicAttributeMapping(
attributeMapping = buildBasicAttributeMapping(
bootPropertyDescriptor.getName(),
role,
attributeIndex,
Expand Down
Loading
Loading