use of org.hibernate.metamodel.spi.RuntimeModelCreationContext in project hibernate-orm by hibernate.
the class EmbeddableMappingTypeImpl method from.
public static EmbeddableMappingTypeImpl from(Component bootDescriptor, CompositeType compositeType, String rootTableExpression, String[] rootTableKeyColumnNames, Function<EmbeddableMappingType, EmbeddableValuedModelPart> embeddedPartBuilder, MappingModelCreationProcess creationProcess) {
final RuntimeModelCreationContext creationContext = creationProcess.getCreationContext();
final EmbeddableMappingTypeImpl mappingType = new EmbeddableMappingTypeImpl(bootDescriptor, embeddedPartBuilder, creationContext);
if (compositeType instanceof CompositeTypeImplementor) {
((CompositeTypeImplementor) compositeType).injectMappingModelPart(mappingType.getEmbeddedValueMapping(), creationProcess);
}
creationProcess.registerInitializationCallback("EmbeddableMappingType(" + mappingType.getNavigableRole().getFullPath() + ")#finishInitialization", () -> mappingType.finishInitialization(bootDescriptor, compositeType, rootTableExpression, rootTableKeyColumnNames, creationProcess));
return mappingType;
}
use of org.hibernate.metamodel.spi.RuntimeModelCreationContext in project hibernate-orm by hibernate.
the class SqmMutationStrategyHelper method resolveInsertStrategy.
/**
* Standard resolution of SqmInsertStrategy to use for a given
* entity hierarchy.
*/
public static SqmMultiTableInsertStrategy resolveInsertStrategy(RootClass entityBootDescriptor, EntityMappingType rootEntityDescriptor, MappingModelCreationProcess creationProcess) {
final RuntimeModelCreationContext creationContext = creationProcess.getCreationContext();
final SessionFactoryImplementor sessionFactory = creationContext.getSessionFactory();
final SessionFactoryOptions options = sessionFactory.getSessionFactoryOptions();
final SqmMultiTableInsertStrategy specifiedStrategy = options.getCustomSqmMultiTableInsertStrategy();
if (specifiedStrategy != null) {
return specifiedStrategy;
}
return sessionFactory.getServiceRegistry().getService(JdbcServices.class).getJdbcEnvironment().getDialect().getFallbackSqmInsertStrategy(rootEntityDescriptor, creationContext);
}
use of org.hibernate.metamodel.spi.RuntimeModelCreationContext in project hibernate-orm by hibernate.
the class PropertyFactory method buildEntityBasedAttribute.
/**
* Generate a non-identifier (and non-version) attribute based on the given mapped property from the given entity
*
* @param property The mapped property.
* @param lazyAvailable Is property lazy loading currently available.
*
* @return The appropriate NonIdentifierProperty definition.
*/
public static NonIdentifierAttribute buildEntityBasedAttribute(EntityPersister persister, SessionFactoryImplementor sessionFactory, int attributeNumber, Property property, boolean lazyAvailable, RuntimeModelCreationContext creationContext) {
final Type type = property.getValue().getType();
final NonIdentifierAttributeNature nature = decode(type);
// we need to dirty check collections, since they can cause an owner
// version number increment
// we need to dirty check many-to-ones with not-found="ignore" in order
// to update the cache (not the database), since in this case a null
// entity reference can lose information
boolean alwaysDirtyCheck = type.isAssociationType() && ((AssociationType) type).isAlwaysDirtyChecked();
SessionFactoryOptions sessionFactoryOptions = sessionFactory.getSessionFactoryOptions();
final boolean lazy = !EnhancementHelper.includeInBaseFetchGroup(property, lazyAvailable, entityName -> {
final MetadataImplementor metadata = creationContext.getMetadata();
final PersistentClass entityBinding = metadata.getEntityBinding(entityName);
assert entityBinding != null;
return entityBinding.hasSubclasses();
}, sessionFactoryOptions.isCollectionsInDefaultFetchGroupEnabled());
switch(nature) {
case BASIC:
{
return new EntityBasedBasicAttribute(persister, sessionFactory, attributeNumber, property.getName(), type, new BaselineAttributeInformation.Builder().setLazy(lazy).setInsertable(property.isInsertable()).setUpdateable(property.isUpdateable()).setValueGenerationStrategy(property.getValueGenerationStrategy()).setNullable(property.isOptional()).setDirtyCheckable(alwaysDirtyCheck || property.isUpdateable()).setVersionable(property.isOptimisticLocked()).setCascadeStyle(property.getCascadeStyle()).setFetchMode(property.getValue().getFetchMode()).createInformation());
}
case COMPOSITE:
{
return new EntityBasedCompositionAttribute(persister, sessionFactory, attributeNumber, property.getName(), (CompositeType) type, new BaselineAttributeInformation.Builder().setLazy(lazy).setInsertable(property.isInsertable()).setUpdateable(property.isUpdateable()).setValueGenerationStrategy(property.getValueGenerationStrategy()).setNullable(property.isOptional()).setDirtyCheckable(alwaysDirtyCheck || property.isUpdateable()).setVersionable(property.isOptimisticLocked()).setCascadeStyle(property.getCascadeStyle()).setFetchMode(property.getValue().getFetchMode()).createInformation());
}
case ENTITY:
case ANY:
case COLLECTION:
{
return new EntityBasedAssociationAttribute(persister, sessionFactory, attributeNumber, property.getName(), (AssociationType) type, new BaselineAttributeInformation.Builder().setLazy(lazy).setInsertable(property.isInsertable()).setUpdateable(property.isUpdateable()).setValueGenerationStrategy(property.getValueGenerationStrategy()).setNullable(property.isOptional()).setDirtyCheckable(alwaysDirtyCheck || property.isUpdateable()).setVersionable(property.isOptimisticLocked()).setCascadeStyle(property.getCascadeStyle()).setFetchMode(property.getValue().getFetchMode()).createInformation());
}
default:
{
throw new HibernateException("Internal error");
}
}
}
use of org.hibernate.metamodel.spi.RuntimeModelCreationContext in project hibernate-orm by hibernate.
the class AbstractEntityPersister method prepareMappingModel.
@Override
public void prepareMappingModel(MappingModelCreationProcess creationProcess) {
if (identifierMapping != null) {
return;
}
final RuntimeModelCreationContext creationContext = creationProcess.getCreationContext();
final PersistentClass bootEntityDescriptor = creationContext.getBootModel().getEntityBinding(getEntityName());
// EntityMappingType rootEntityDescriptor;
if (superMappingType != null) {
((InFlightEntityMappingType) superMappingType).prepareMappingModel(creationProcess);
if (shouldProcessSuperMapping()) {
this.discriminatorMapping = superMappingType.getDiscriminatorMapping();
this.identifierMapping = superMappingType.getIdentifierMapping();
this.naturalIdMapping = superMappingType.getNaturalIdMapping();
this.versionMapping = superMappingType.getVersionMapping();
this.rowIdMapping = superMappingType.getRowIdMapping();
} else {
prepareMappingModel(creationProcess, bootEntityDescriptor);
}
// rootEntityDescriptor = superMappingType.getRootEntityDescriptor();
} else {
prepareMappingModel(creationProcess, bootEntityDescriptor);
// rootEntityDescriptor = this;
}
final EntityMetamodel currentEntityMetamodel = this.getEntityMetamodel();
int stateArrayPosition = getStateArrayInitialPosition(creationProcess);
NonIdentifierAttribute[] properties = currentEntityMetamodel.getProperties();
for (int i = 0; i < currentEntityMetamodel.getPropertySpan(); i++) {
final NonIdentifierAttribute runtimeAttrDefinition = properties[i];
final Property bootProperty = bootEntityDescriptor.getProperty(runtimeAttrDefinition.getName());
if (superMappingType == null || superMappingType.findAttributeMapping(bootProperty.getName()) == null) {
declaredAttributeMappings.put(runtimeAttrDefinition.getName(), generateNonIdAttributeMapping(runtimeAttrDefinition, bootProperty, stateArrayPosition++, creationProcess));
}
// else {
// its defined on the supertype, skip it here
// }
}
getAttributeMappings();
postProcessAttributeMappings(creationProcess, bootEntityDescriptor);
final ReflectionOptimizer reflectionOptimizer = representationStrategy.getReflectionOptimizer();
if (reflectionOptimizer != null) {
accessOptimizer = reflectionOptimizer.getAccessOptimizer();
} else {
accessOptimizer = null;
}
// register a callback for after all `#prepareMappingModel` calls have finished. here we want to delay the
// generation of `staticFetchableList` because we need to wait until after all sub-classes have had their
// `#prepareMappingModel` called (and their declared attribute mappings resolved)
creationProcess.registerInitializationCallback("Entity(" + getEntityName() + ") `staticFetchableList` generator", () -> {
if (hasInsertGeneratedProperties()) {
insertGeneratedValuesProcessor = createGeneratedValuesProcessor(GenerationTiming.INSERT);
}
if (hasUpdateGeneratedProperties()) {
updateGeneratedValuesProcessor = createGeneratedValuesProcessor(GenerationTiming.ALWAYS);
}
staticFetchableList = new ArrayList<>(attributeMappings.size());
visitSubTypeAttributeMappings(attributeMapping -> staticFetchableList.add(attributeMapping));
return true;
});
boolean needsMultiTableInsert = hasMultipleTables();
if (needsMultiTableInsert) {
creationProcess.registerInitializationCallback("Entity(" + getEntityName() + ") `sqmMultiTableMutationStrategy` interpretation", () -> {
sqmMultiTableMutationStrategy = interpretSqmMultiTableStrategy(this, creationProcess);
if (sqmMultiTableMutationStrategy == null) {
return false;
}
sqmMultiTableMutationStrategy.prepare(creationProcess, creationContext.getSessionFactory().getJdbcServices().getBootstrapJdbcConnectionAccess());
return true;
});
} else {
sqmMultiTableMutationStrategy = null;
}
if (!needsMultiTableInsert && getIdentifierGenerator() instanceof BulkInsertionCapableIdentifierGenerator) {
if (getIdentifierGenerator() instanceof OptimizableGenerator) {
final Optimizer optimizer = ((OptimizableGenerator) getIdentifierGenerator()).getOptimizer();
needsMultiTableInsert = optimizer != null && optimizer.getIncrementSize() > 1;
}
}
if (needsMultiTableInsert) {
creationProcess.registerInitializationCallback("Entity(" + getEntityName() + ") `sqmMultiTableInsertStrategy` interpretation", () -> {
sqmMultiTableInsertStrategy = interpretSqmMultiTableInsertStrategy(this, creationProcess);
if (sqmMultiTableInsertStrategy == null) {
return false;
}
sqmMultiTableInsertStrategy.prepare(creationProcess, creationContext.getSessionFactory().getJdbcServices().getBootstrapJdbcConnectionAccess());
return true;
});
} else {
sqmMultiTableInsertStrategy = null;
}
}
use of org.hibernate.metamodel.spi.RuntimeModelCreationContext in project hibernate-orm by hibernate.
the class JpaMetamodelImpl method processJpa.
public void processJpa(MetadataImplementor bootMetamodel, MappingMetamodel mappingMetamodel, Map<Class<?>, String> entityProxyInterfaceMap, JpaStaticMetaModelPopulationSetting jpaStaticMetaModelPopulationSetting, JpaMetaModelPopulationSetting jpaMetaModelPopulationSetting, Collection<NamedEntityGraphDefinition> namedEntityGraphDefinitions, RuntimeModelCreationContext runtimeModelCreationContext) {
bootMetamodel.getImports().forEach((k, v) -> this.nameToImportMap.put(k, new ImportInfo<>(v, null)));
this.entityProxyInterfaceMap.putAll(entityProxyInterfaceMap);
final MetadataContext context = new MetadataContext(this, mappingMetamodel, bootMetamodel, jpaStaticMetaModelPopulationSetting, jpaMetaModelPopulationSetting, runtimeModelCreationContext);
for (PersistentClass entityBinding : bootMetamodel.getEntityBindings()) {
locateOrBuildEntityType(entityBinding, context, typeConfiguration);
}
handleUnusedMappedSuperclasses(context, typeConfiguration);
context.wrapUp();
for (Map.Entry<String, IdentifiableDomainType<?>> entry : context.getIdentifiableTypesByName().entrySet()) {
if (entry.getValue() instanceof EntityDomainType<?>) {
this.jpaEntityTypeMap.put(entry.getKey(), (EntityDomainType<?>) entry.getValue());
}
}
this.jpaManagedTypeMap.putAll(context.getEntityTypeMap());
this.jpaManagedTypeMap.putAll(context.getMappedSuperclassTypeMap());
switch(jpaMetaModelPopulationSetting) {
case IGNORE_UNSUPPORTED:
this.jpaManagedTypes.addAll(context.getEntityTypeMap().values());
this.jpaManagedTypes.addAll(context.getMappedSuperclassTypeMap().values());
break;
case ENABLED:
this.jpaManagedTypes.addAll(context.getIdentifiableTypesByName().values());
break;
}
for (EmbeddableDomainType<?> embeddable : context.getEmbeddableTypeSet()) {
switch(jpaMetaModelPopulationSetting) {
case IGNORE_UNSUPPORTED:
if (embeddable.getJavaType() != null && embeddable.getJavaType() != Map.class) {
this.jpaEmbeddables.add(embeddable);
this.jpaManagedTypes.add(embeddable);
if (!(embeddable.getExpressibleJavaType() instanceof EntityJavaType<?>)) {
this.jpaManagedTypeMap.put(embeddable.getJavaType(), embeddable);
}
}
break;
case ENABLED:
this.jpaEmbeddables.add(embeddable);
this.jpaManagedTypes.add(embeddable);
if (embeddable.getJavaType() != null && !(embeddable.getExpressibleJavaType() instanceof EntityJavaType<?>)) {
this.jpaManagedTypeMap.put(embeddable.getJavaType(), embeddable);
}
break;
case DISABLED:
if (embeddable.getJavaType() == null) {
throw new UnsupportedOperationException("ANY not supported");
}
if (!(embeddable.getExpressibleJavaType() instanceof EntityJavaType<?>)) {
this.jpaManagedTypeMap.put(embeddable.getJavaType(), embeddable);
}
break;
}
}
final Consumer<PersistentAttribute<?, ?>> attributeConsumer = persistentAttribute -> {
if (persistentAttribute.getJavaType() != null && persistentAttribute.getJavaType().isEnum()) {
@SuppressWarnings("unchecked") final Class<Enum<?>> enumClass = (Class<Enum<?>>) persistentAttribute.getJavaType();
final Enum<?>[] enumConstants = enumClass.getEnumConstants();
for (Enum<?> enumConstant : enumConstants) {
final String qualifiedEnumLiteral = enumConstant.getDeclaringClass().getSimpleName() + "." + enumConstant.name();
this.allowedEnumLiteralTexts.computeIfAbsent(enumConstant.name(), k -> new HashMap<>()).put(enumClass, enumConstant);
this.allowedEnumLiteralTexts.computeIfAbsent(qualifiedEnumLiteral, k -> new HashMap<>()).put(enumClass, enumConstant);
}
}
};
domainTypeStream(context).forEach(managedDomainType -> managedDomainType.visitAttributes(attributeConsumer));
applyNamedEntityGraphs(namedEntityGraphDefinitions);
}
Aggregations