HHH-17202 Fix ArrayStoreException for single field id class entity collection batch loading

This commit is contained in:
Christian Beikov 2023-09-14 10:35:18 +02:00
parent a27d7994a6
commit 30687e0370
1 changed files with 33 additions and 18 deletions

View File

@ -19,7 +19,10 @@ import org.hibernate.loader.ast.spi.CollectionBatchLoader;
import org.hibernate.loader.ast.spi.SqlArrayMultiKeyLoader; import org.hibernate.loader.ast.spi.SqlArrayMultiKeyLoader;
import org.hibernate.metamodel.mapping.ForeignKeyDescriptor; import org.hibernate.metamodel.mapping.ForeignKeyDescriptor;
import org.hibernate.metamodel.mapping.JdbcMapping; import org.hibernate.metamodel.mapping.JdbcMapping;
import org.hibernate.metamodel.mapping.NonAggregatedIdentifierMapping;
import org.hibernate.metamodel.mapping.PluralAttributeMapping; import org.hibernate.metamodel.mapping.PluralAttributeMapping;
import org.hibernate.metamodel.mapping.ValuedModelPart;
import org.hibernate.metamodel.mapping.internal.IdClassEmbeddable;
import org.hibernate.query.spi.QueryOptions; import org.hibernate.query.spi.QueryOptions;
import org.hibernate.sql.ast.tree.expression.JdbcParameter; import org.hibernate.sql.ast.tree.expression.JdbcParameter;
import org.hibernate.sql.ast.tree.select.SelectStatement; import org.hibernate.sql.ast.tree.select.SelectStatement;
@ -43,7 +46,7 @@ import static org.hibernate.loader.ast.internal.MultiKeyLoadLogging.MULTI_KEY_LO
public class CollectionBatchLoaderArrayParam public class CollectionBatchLoaderArrayParam
extends AbstractCollectionBatchLoader extends AbstractCollectionBatchLoader
implements CollectionBatchLoader, SqlArrayMultiKeyLoader { implements CollectionBatchLoader, SqlArrayMultiKeyLoader {
private final Class<?> arrayElementType; private final Class<?> keyDomainType;
private final JdbcMapping arrayJdbcMapping; private final JdbcMapping arrayJdbcMapping;
private final JdbcParameter jdbcParameter; private final JdbcParameter jdbcParameter;
private final SelectStatement sqlSelect; private final SelectStatement sqlSelect;
@ -65,17 +68,18 @@ public class CollectionBatchLoaderArrayParam
} }
final ForeignKeyDescriptor keyDescriptor = getLoadable().getKeyDescriptor(); final ForeignKeyDescriptor keyDescriptor = getLoadable().getKeyDescriptor();
final JdbcMapping jdbcMapping = keyDescriptor.getSingleJdbcMapping();
arrayElementType = keyDescriptor.getJavaType().getJavaTypeClass(); final Class<?> jdbcArrayClass = Array.newInstance( jdbcMapping.getJdbcJavaType().getJavaTypeClass(), 0 )
Class<?> arrayClass = Array.newInstance( arrayElementType, 0 ).getClass(); .getClass();
keyDomainType = getKeyType( keyDescriptor.getKeyPart() );
final BasicType<?> arrayBasicType = getSessionFactory().getTypeConfiguration() final BasicType<?> arrayBasicType = getSessionFactory().getTypeConfiguration()
.getBasicTypeRegistry() .getBasicTypeRegistry()
.getRegisteredType( arrayClass ); .getRegisteredType( jdbcArrayClass );
arrayJdbcMapping = MultiKeyLoadHelper.resolveArrayJdbcMapping( arrayJdbcMapping = MultiKeyLoadHelper.resolveArrayJdbcMapping(
arrayBasicType, arrayBasicType,
keyDescriptor.getSingleJdbcMapping(), jdbcMapping,
arrayClass, jdbcArrayClass,
getSessionFactory() getSessionFactory()
); );
@ -96,6 +100,16 @@ public class CollectionBatchLoaderArrayParam
.translate( JdbcParameterBindings.NO_BINDINGS, QueryOptions.NONE ); .translate( JdbcParameterBindings.NO_BINDINGS, QueryOptions.NONE );
} }
private Class<?> getKeyType(ValuedModelPart keyPart) {
if ( keyPart instanceof NonAggregatedIdentifierMapping ) {
final IdClassEmbeddable idClassEmbeddable = ( (NonAggregatedIdentifierMapping) keyPart ).getIdClassEmbeddable();
if ( idClassEmbeddable != null ) {
return idClassEmbeddable.getMappedJavaType().getJavaTypeClass();
}
}
return keyPart.getJavaType().getJavaTypeClass();
}
@Override @Override
public PersistentCollection<?> load(Object key, SharedSessionContractImplementor session) { public PersistentCollection<?> load(Object key, SharedSessionContractImplementor session) {
if ( MULTI_KEY_LOAD_LOGGER.isDebugEnabled() ) { if ( MULTI_KEY_LOAD_LOGGER.isDebugEnabled() ) {
@ -126,20 +140,21 @@ public class CollectionBatchLoaderArrayParam
ForeignKeyDescriptor keyDescriptor) { ForeignKeyDescriptor keyDescriptor) {
final int length = getDomainBatchSize(); final int length = getDomainBatchSize();
final Object[] keysToInitialize = (Object[]) Array.newInstance( final Object[] jdbcKeysToInitialize = (Object[]) Array.newInstance(
keyDescriptor.getSingleJdbcMapping().getJdbcJavaType().getJavaTypeClass(), jdbcParameter.getExpressionType()
length .getSingleJdbcMapping()
); .getJdbcJavaType()
final Object[] embeddedKeys = (Object[]) Array.newInstance( .getJavaTypeClass()
arrayElementType, .getComponentType(),
length length
); );
final Object[] embeddedKeys = (Object[]) Array.newInstance( keyDomainType, length );
session.getPersistenceContextInternal().getBatchFetchQueue() session.getPersistenceContextInternal().getBatchFetchQueue()
.collectBatchLoadableCollectionKeys( .collectBatchLoadableCollectionKeys(
length, length,
(index, key) -> (index, key) ->
keyDescriptor.forEachJdbcValue( key, (i, value, jdbcMapping) -> { keyDescriptor.forEachJdbcValue( key, (i, value, jdbcMapping) -> {
keysToInitialize[index] = value; jdbcKeysToInitialize[index] = value;
embeddedKeys[index] = key; embeddedKeys[index] = key;
}, session ) }, session )
, ,
@ -147,7 +162,7 @@ public class CollectionBatchLoaderArrayParam
getLoadable() getLoadable()
); );
initializeKeys( keysToInitialize, session ); initializeKeys( jdbcKeysToInitialize, session );
for ( Object initializedKey : embeddedKeys ) { for ( Object initializedKey : embeddedKeys ) {
finishInitializingKey( initializedKey, session ); finishInitializingKey( initializedKey, session );
@ -155,13 +170,13 @@ public class CollectionBatchLoaderArrayParam
final CollectionKey collectionKey = new CollectionKey( final CollectionKey collectionKey = new CollectionKey(
getLoadable().getCollectionDescriptor(), getLoadable().getCollectionDescriptor(),
keysToInitialize keyBeingLoaded
); );
return session.getPersistenceContext().getCollection( collectionKey ); return session.getPersistenceContext().getCollection( collectionKey );
} }
private Object[] resolveKeysToInitialize(Object keyBeingLoaded, SharedSessionContractImplementor session) { private Object[] resolveKeysToInitialize(Object keyBeingLoaded, SharedSessionContractImplementor session) {
final Object[] keysToInitialize = (Object[]) Array.newInstance( arrayElementType, getDomainBatchSize() ); final Object[] keysToInitialize = (Object[]) Array.newInstance( keyDomainType, getDomainBatchSize() );
session.getPersistenceContextInternal().getBatchFetchQueue().collectBatchLoadableCollectionKeys( session.getPersistenceContextInternal().getBatchFetchQueue().collectBatchLoadableCollectionKeys(
getDomainBatchSize(), getDomainBatchSize(),
(index, value) -> keysToInitialize[index] = value, (index, value) -> keysToInitialize[index] = value,