use of org.hibernate.metamodel.model.domain.internal.AttributeContainer in project hibernate-orm by hibernate.
the class MetadataContext method applyIdMetadata.
private <X> void applyIdMetadata(MappedSuperclass mappingType, MappedSuperclassDomainType<X> jpaMappingType) {
if (mappingType.hasIdentifierProperty()) {
final Property declaredIdentifierProperty = mappingType.getDeclaredIdentifierProperty();
if (declaredIdentifierProperty != null) {
final SingularPersistentAttribute<X, Object> attribute = attributeFactory.buildIdAttribute(jpaMappingType, declaredIdentifierProperty);
// noinspection unchecked
((AttributeContainer) jpaMappingType).getInFlightAccess().applyIdAttribute(attribute);
}
} else // a MappedSuperclass can have no identifier if the id is set below in the hierarchy
if (mappingType.getIdentifierMapper() != null) {
Set<SingularPersistentAttribute<? super X, ?>> attributes = buildIdClassAttributes(jpaMappingType, mappingType.getIdentifierMapper().getProperties());
// noinspection unchecked
((AttributeContainer<X>) jpaMappingType).getInFlightAccess().applyIdClassAttributes(attributes);
}
}
use of org.hibernate.metamodel.model.domain.internal.AttributeContainer in project hibernate-orm by hibernate.
the class MetadataContext method wrapUp.
@SuppressWarnings("unchecked")
public void wrapUp() {
if (LOG.isTraceEnabled()) {
LOG.trace("Wrapping up metadata context...");
}
boolean staticMetamodelScanEnabled = this.jpaStaticMetaModelPopulationSetting != JpaStaticMetaModelPopulationSetting.DISABLED;
// we need to process types from superclasses to subclasses
for (Object mapping : orderedMappings) {
if (PersistentClass.class.isAssignableFrom(mapping.getClass())) {
final PersistentClass safeMapping = (PersistentClass) mapping;
if (LOG.isTraceEnabled()) {
LOG.trace("Starting entity [" + safeMapping.getEntityName() + ']');
}
try {
final EntityDomainType<Object> jpaMapping = (EntityDomainType<Object>) entityTypesByPersistentClass.get(safeMapping);
applyIdMetadata(safeMapping, jpaMapping);
applyVersionAttribute(safeMapping, jpaMapping);
for (Property property : safeMapping.getDeclaredProperties()) {
if (property.getValue() == safeMapping.getIdentifierMapper()) {
// #buildIdClassAttributes
continue;
}
if (safeMapping.isVersioned() && property == safeMapping.getVersion()) {
// skip the version property, it was already handled previously.
continue;
}
final PersistentAttribute<Object, ?> attribute = attributeFactory.buildAttribute(jpaMapping, property);
if (attribute != null) {
addAttribute(jpaMapping, attribute);
if (property.isNaturalIdentifier()) {
((AttributeContainer<Object>) jpaMapping).getInFlightAccess().applyNaturalIdAttribute(attribute);
}
}
}
((AttributeContainer<?>) jpaMapping).getInFlightAccess().finishUp();
if (staticMetamodelScanEnabled) {
populateStaticMetamodel(jpaMapping);
}
} finally {
if (LOG.isTraceEnabled()) {
LOG.trace("Completed entity [" + safeMapping.getEntityName() + ']');
}
}
} else if (MappedSuperclass.class.isAssignableFrom(mapping.getClass())) {
final MappedSuperclass safeMapping = (MappedSuperclass) mapping;
if (LOG.isTraceEnabled()) {
LOG.trace("Starting mapped superclass [" + safeMapping.getMappedClass().getName() + ']');
}
try {
final MappedSuperclassDomainType<Object> jpaType = (MappedSuperclassDomainType<Object>) mappedSuperclassByMappedSuperclassMapping.get(safeMapping);
applyIdMetadata(safeMapping, jpaType);
applyVersionAttribute(safeMapping, jpaType);
for (Property property : safeMapping.getDeclaredProperties()) {
if (safeMapping.isVersioned() && property == safeMapping.getVersion()) {
// skip the version property, it was already handled previously.
continue;
}
final PersistentAttribute<Object, ?> attribute = attributeFactory.buildAttribute(jpaType, property);
if (attribute != null) {
addAttribute(jpaType, attribute);
if (property.isNaturalIdentifier()) {
((AttributeContainer<Object>) jpaType).getInFlightAccess().applyNaturalIdAttribute(attribute);
}
}
}
((AttributeContainer<?>) jpaType).getInFlightAccess().finishUp();
if (staticMetamodelScanEnabled) {
populateStaticMetamodel(jpaType);
}
} finally {
if (LOG.isTraceEnabled()) {
LOG.trace("Completed mapped superclass [" + safeMapping.getMappedClass().getName() + ']');
}
}
} else {
throw new AssertionFailure("Unexpected mapping type: " + mapping.getClass());
}
}
while (!embeddablesToProcess.isEmpty()) {
final ArrayList<EmbeddableDomainType<?>> processingEmbeddables = new ArrayList<>(embeddablesToProcess.size());
for (List<EmbeddableDomainType<?>> embeddableDomainTypes : embeddablesToProcess.values()) {
processingEmbeddables.addAll(embeddableDomainTypes);
}
embeddablesToProcess.clear();
for (EmbeddableDomainType<?> embeddable : processingEmbeddables) {
final Component component = componentByEmbeddable.get(embeddable);
for (Property property : component.getProperties()) {
final PersistentAttribute<Object, ?> attribute = attributeFactory.buildAttribute((ManagedDomainType<Object>) embeddable, property);
if (attribute != null) {
addAttribute(embeddable, attribute);
}
}
((AttributeContainer<?>) embeddable).getInFlightAccess().finishUp();
embeddables.put(embeddable.getJavaType(), embeddable);
if (staticMetamodelScanEnabled) {
populateStaticMetamodel(embeddable);
}
}
}
}
use of org.hibernate.metamodel.model.domain.internal.AttributeContainer in project hibernate-orm by hibernate.
the class MetadataContext method applyIdMetadata.
// 1) create the part
// 2) register the part (mapping role)
// 3) somehow get the mapping role "into" the part (setter, ?)
private void applyIdMetadata(PersistentClass persistentClass, IdentifiableDomainType<?> identifiableType) {
if (persistentClass.hasIdentifierProperty()) {
final Property declaredIdentifierProperty = persistentClass.getDeclaredIdentifierProperty();
if (declaredIdentifierProperty != null) {
final SingularPersistentAttribute<?, Object> idAttribute = attributeFactory.buildIdAttribute(identifiableType, declaredIdentifierProperty);
// noinspection unchecked rawtypes
((AttributeContainer) identifiableType).getInFlightAccess().applyIdAttribute(idAttribute);
}
} else {
if (!(persistentClass.getIdentifier() instanceof Component)) {
throw new MappingException("Expecting Component for id mapping with no id-attribute");
}
// Handle the actual id-attributes
final Component cidValue = (Component) persistentClass.getIdentifier();
final List<Property> cidProperties;
final int propertySpan;
final EmbeddableTypeImpl<?> idClassType;
final Component identifierMapper = persistentClass.getIdentifierMapper();
if (identifierMapper != null) {
cidProperties = identifierMapper.getProperties();
propertySpan = identifierMapper.getPropertySpan();
idClassType = applyIdClassMetadata((Component) persistentClass.getIdentifier());
} else {
cidProperties = cidValue.getProperties();
propertySpan = cidValue.getPropertySpan();
idClassType = null;
}
assert cidValue.isEmbedded();
AbstractIdentifiableType<?> idType = (AbstractIdentifiableType<?>) identifiableTypesByName.get(cidValue.getOwner().getEntityName());
// noinspection rawtypes
Set idAttributes = idType.getIdClassAttributesSafely();
if (idAttributes == null) {
idAttributes = new HashSet<>(propertySpan);
for (Property cidSubproperty : cidProperties) {
final SingularPersistentAttribute<?, Object> cidSubAttr = attributeFactory.buildIdAttribute(idType, cidSubproperty);
// noinspection unchecked
idAttributes.add(cidSubAttr);
}
}
AttributeContainer<?> container = (AttributeContainer<?>) identifiableType;
// noinspection unchecked
container.getInFlightAccess().applyNonAggregatedIdAttributes(idAttributes, idClassType);
}
}
use of org.hibernate.metamodel.model.domain.internal.AttributeContainer in project hibernate-orm by hibernate.
the class MetadataContext method addAttribute.
private void addAttribute(ManagedDomainType<?> type, PersistentAttribute<Object, ?> attribute) {
// noinspection unchecked
AttributeContainer<Object> container = (AttributeContainer<Object>) type;
final AttributeContainer.InFlightAccess<Object> inFlightAccess = container.getInFlightAccess();
final boolean virtual = attribute.getPersistentAttributeType() == Attribute.PersistentAttributeType.EMBEDDED && attribute.getAttributeJavaType() instanceof EntityJavaType<?>;
if (virtual) {
final EmbeddableDomainType<?> embeddableDomainType = (EmbeddableDomainType<?>) attribute.getValueGraphType();
final Component component = componentByEmbeddable.get(embeddableDomainType);
for (Property property : component.getProperties()) {
// noinspection unchecked
ManagedDomainType<Object> managedDomainType = (ManagedDomainType<Object>) embeddableDomainType;
final PersistentAttribute<Object, ?> subAttribute = attributeFactory.buildAttribute(managedDomainType, property);
if (subAttribute != null) {
inFlightAccess.addAttribute(subAttribute);
}
}
if (jpaMetaModelPopulationSetting != JpaMetaModelPopulationSetting.ENABLED) {
return;
}
}
inFlightAccess.addAttribute(attribute);
}
Aggregations