use of org.hibernate.metamodel.spi.RuntimeModelCreationContext in project hibernate-orm by hibernate.
the class MappingMetamodelImpl method finishInitialization.
public void finishInitialization(MetadataImplementor bootModel, BootstrapContext bootstrapContext, SessionFactoryImplementor sessionFactory) {
final RuntimeModelCreationContext runtimeModelCreationContext = new RuntimeModelCreationContext() {
@Override
public BootstrapContext getBootstrapContext() {
return bootstrapContext;
}
@Override
public SessionFactoryImplementor getSessionFactory() {
return sessionFactory;
}
@Override
public MetadataImplementor getBootModel() {
return bootModel;
}
@Override
public MappingMetamodel getDomainModel() {
return MappingMetamodelImpl.this;
}
};
final PersisterFactory persisterFactory = sessionFactory.getServiceRegistry().getService(PersisterFactory.class);
final JpaStaticMetaModelPopulationSetting jpaStaticMetaModelPopulationSetting = determineJpaStaticMetaModelPopulationSetting(sessionFactory.getProperties());
final JpaMetaModelPopulationSetting jpaMetaModelPopulationSetting = determineJpaMetaModelPopulationSetting(sessionFactory.getProperties());
bootModel.visitRegisteredComponents(Component::prepareForMappingModel);
bootModel.getMappedSuperclassMappingsCopy().forEach(MappedSuperclass::prepareForMappingModel);
bootModel.getEntityBindings().forEach(PersistentClass::prepareForMappingModel);
processBootEntities(bootModel.getEntityBindings(), sessionFactory.getCache(), persisterFactory, runtimeModelCreationContext);
processBootCollections(bootModel.getCollectionBindings(), sessionFactory.getCache(), persisterFactory, runtimeModelCreationContext);
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// after *all* persisters and named queries are registered
MappingModelCreationProcess.process(entityPersisterMap, sessionFactory.getQueryEngine().getSqmFunctionRegistry(), runtimeModelCreationContext);
for (EntityPersister persister : entityPersisterMap.values()) {
persister.postInstantiate();
registerEntityNameResolvers(persister, entityNameResolvers);
}
collectionPersisterMap.values().forEach(CollectionPersister::postInstantiate);
((JpaMetamodelImpl) this.jpaMetamodel).processJpa(bootModel, this, entityProxyInterfaceMap, jpaStaticMetaModelPopulationSetting, jpaMetaModelPopulationSetting, bootModel.getNamedEntityGraphs().values(), runtimeModelCreationContext);
}
use of org.hibernate.metamodel.spi.RuntimeModelCreationContext in project hibernate-orm by hibernate.
the class MappingModelCreationHelper method buildPluralAttributeMapping.
@SuppressWarnings("rawtypes")
public static PluralAttributeMapping buildPluralAttributeMapping(String attrName, int stateArrayPosition, Property bootProperty, ManagedMappingType declaringType, PropertyAccess propertyAccess, CascadeStyle cascadeStyle, FetchMode fetchMode, MappingModelCreationProcess creationProcess) {
final Collection bootValueMapping = (Collection) bootProperty.getValue();
final RuntimeModelCreationContext creationContext = creationProcess.getCreationContext();
final SessionFactoryImplementor sessionFactory = creationContext.getSessionFactory();
final SqlStringGenerationContext sqlStringGenerationContext = sessionFactory.getSqlStringGenerationContext();
final Dialect dialect = sqlStringGenerationContext.getDialect();
final MappingMetamodel domainModel = creationContext.getDomainModel();
final CollectionPersister collectionDescriptor = domainModel.findCollectionDescriptor(bootValueMapping.getRole());
assert collectionDescriptor != null;
final String tableExpression = ((Joinable) collectionDescriptor).getTableName();
final String sqlAliasStem = SqlAliasStemHelper.INSTANCE.generateStemFromAttributeName(bootProperty.getName());
final CollectionMappingType<?> collectionMappingType;
final JavaTypeRegistry jtdRegistry = creationContext.getJavaTypeRegistry();
final CollectionPart elementDescriptor = interpretElement(bootValueMapping, tableExpression, collectionDescriptor, sqlAliasStem, dialect, creationProcess);
final CollectionPart indexDescriptor;
CollectionIdentifierDescriptor identifierDescriptor = null;
final CollectionSemantics<?, ?> collectionSemantics = collectionDescriptor.getCollectionSemantics();
switch(collectionSemantics.getCollectionClassification()) {
case ARRAY:
{
collectionMappingType = new CollectionMappingTypeImpl(jtdRegistry.getDescriptor(Object[].class), StandardArraySemantics.INSTANCE);
final BasicValue index = (BasicValue) ((IndexedCollection) bootValueMapping).getIndex();
final SelectableMapping selectableMapping = SelectableMappingImpl.from(tableExpression, index.getSelectables().get(0), creationContext.getTypeConfiguration().getBasicTypeForJavaType(Integer.class), dialect, creationProcess.getSqmFunctionRegistry());
indexDescriptor = new BasicValuedCollectionPart(collectionDescriptor, CollectionPart.Nature.INDEX, // no converter
null, selectableMapping);
break;
}
case BAG:
{
collectionMappingType = new CollectionMappingTypeImpl(jtdRegistry.getDescriptor(java.util.Collection.class), StandardBagSemantics.INSTANCE);
indexDescriptor = null;
break;
}
case ID_BAG:
{
collectionMappingType = new CollectionMappingTypeImpl(jtdRegistry.getDescriptor(java.util.Collection.class), StandardIdentifierBagSemantics.INSTANCE);
indexDescriptor = null;
assert collectionDescriptor instanceof SQLLoadableCollection;
final SQLLoadableCollection loadableCollection = (SQLLoadableCollection) collectionDescriptor;
final String identifierColumnName = loadableCollection.getIdentifierColumnName();
assert identifierColumnName != null;
identifierDescriptor = new CollectionIdentifierDescriptorImpl(collectionDescriptor, tableExpression, identifierColumnName, (BasicType) loadableCollection.getIdentifierType());
break;
}
case LIST:
{
final BasicValue index = (BasicValue) ((IndexedCollection) bootValueMapping).getIndex();
final SelectableMapping selectableMapping = SelectableMappingImpl.from(tableExpression, index.getSelectables().get(0), creationContext.getTypeConfiguration().getBasicTypeForJavaType(Integer.class), dialect, creationProcess.getSqmFunctionRegistry());
indexDescriptor = new BasicValuedCollectionPart(collectionDescriptor, CollectionPart.Nature.INDEX, // no converter
null, selectableMapping);
collectionMappingType = new CollectionMappingTypeImpl(jtdRegistry.getDescriptor(List.class), StandardListSemantics.INSTANCE);
break;
}
case MAP:
case ORDERED_MAP:
case SORTED_MAP:
{
final Class<? extends java.util.Map> mapJavaType = collectionSemantics.getCollectionClassification() == CollectionClassification.SORTED_MAP ? SortedMap.class : java.util.Map.class;
collectionMappingType = new CollectionMappingTypeImpl(jtdRegistry.getDescriptor(mapJavaType), collectionSemantics);
final String mapKeyTableExpression;
if (bootValueMapping instanceof Map && ((Map) bootValueMapping).getMapKeyPropertyName() != null) {
mapKeyTableExpression = getTableIdentifierExpression(((Map) bootValueMapping).getIndex().getTable(), creationProcess);
} else {
mapKeyTableExpression = tableExpression;
}
indexDescriptor = interpretMapKey(bootValueMapping, collectionDescriptor, mapKeyTableExpression, sqlAliasStem, dialect, creationProcess);
break;
}
case SET:
case ORDERED_SET:
case SORTED_SET:
{
final Class<? extends java.util.Set> setJavaType = collectionSemantics.getCollectionClassification() == CollectionClassification.SORTED_MAP ? SortedSet.class : java.util.Set.class;
collectionMappingType = new CollectionMappingTypeImpl(jtdRegistry.getDescriptor(setJavaType), collectionSemantics);
indexDescriptor = null;
break;
}
default:
{
throw new MappingException("Unexpected CollectionClassification : " + collectionSemantics.getCollectionClassification());
}
}
final StateArrayContributorMetadata contributorMetadata = new StateArrayContributorMetadata() {
@Override
public PropertyAccess getPropertyAccess() {
return propertyAccess;
}
@Override
public MutabilityPlan getMutabilityPlan() {
return ImmutableMutabilityPlan.instance();
}
@Override
public boolean isNullable() {
return bootProperty.isOptional();
}
@Override
public boolean isInsertable() {
return bootProperty.isInsertable();
}
@Override
public boolean isUpdatable() {
return bootProperty.isUpdateable();
}
@Override
public boolean isIncludedInDirtyChecking() {
return false;
}
@Override
public boolean isIncludedInOptimisticLocking() {
return bootProperty.isOptimisticLocked();
}
@Override
public CascadeStyle getCascadeStyle() {
return cascadeStyle;
}
};
final FetchStyle style = FetchOptionsHelper.determineFetchStyleByMetadata(fetchMode, collectionDescriptor.getCollectionType(), sessionFactory);
final FetchTiming timing = FetchOptionsHelper.determineFetchTiming(style, collectionDescriptor.getCollectionType(), collectionDescriptor.isLazy(), collectionDescriptor.getRole(), sessionFactory);
final PluralAttributeMappingImpl pluralAttributeMapping = new PluralAttributeMappingImpl(attrName, bootValueMapping, propertyAccess, entityMappingType -> contributorMetadata, collectionMappingType, stateArrayPosition, elementDescriptor, indexDescriptor, identifierDescriptor, timing, style, cascadeStyle, declaringType, collectionDescriptor);
creationProcess.registerInitializationCallback("PluralAttributeMapping(" + bootValueMapping.getRole() + ")#finishInitialization", () -> {
pluralAttributeMapping.finishInitialization(bootProperty, bootValueMapping, creationProcess);
return true;
});
creationProcess.registerInitializationCallback("PluralAttributeMapping(" + bootValueMapping.getRole() + ") - key descriptor", () -> {
interpretPluralAttributeMappingKeyDescriptor(pluralAttributeMapping, bootValueMapping, collectionDescriptor, declaringType, dialect, creationProcess);
return true;
});
return pluralAttributeMapping;
}
use of org.hibernate.metamodel.spi.RuntimeModelCreationContext in project hibernate-orm by hibernate.
the class SqmMutationStrategyHelper method resolveStrategy.
/**
* Standard resolution of SqmMutationStrategy to use for a given
* entity hierarchy.
*/
public static SqmMultiTableMutationStrategy resolveStrategy(RootClass entityBootDescriptor, EntityMappingType rootEntityDescriptor, MappingModelCreationProcess creationProcess) {
final RuntimeModelCreationContext creationContext = creationProcess.getCreationContext();
final SessionFactoryImplementor sessionFactory = creationContext.getSessionFactory();
final SessionFactoryOptions options = sessionFactory.getSessionFactoryOptions();
final SqmMultiTableMutationStrategy specifiedStrategy = options.getCustomSqmMultiTableMutationStrategy();
if (specifiedStrategy != null) {
return specifiedStrategy;
}
return sessionFactory.getServiceRegistry().getService(JdbcServices.class).getJdbcEnvironment().getDialect().getFallbackSqmMutationStrategy(rootEntityDescriptor, creationContext);
}
Aggregations