use of org.hibernate.metamodel.mapping.CompositeIdentifierMapping in project hibernate-orm by hibernate.
the class DefaultLoadEventListener method checkIdClass.
private void checkIdClass(final EntityPersister persister, final LoadEvent event, final LoadType loadType, final Class<?> idClass) {
// we may have the jpa requirement of allowing find-by-id where id is the "simple pk value" of a
// dependent objects parent. This is part of its generally goofy derived identity "feature"
final EntityIdentifierMapping idMapping = persister.getIdentifierMapping();
if (idMapping instanceof CompositeIdentifierMapping) {
final CompositeIdentifierMapping compositeIdMapping = (CompositeIdentifierMapping) idMapping;
final List<AttributeMapping> attributeMappings = compositeIdMapping.getPartMappingType().getAttributeMappings();
if (attributeMappings.size() == 1) {
final AttributeMapping singleIdAttribute = attributeMappings.get(0);
if (singleIdAttribute.getMappedType() instanceof EntityMappingType) {
final EntityMappingType parentIdTargetMapping = (EntityMappingType) singleIdAttribute.getMappedType();
final EntityIdentifierMapping parentIdTargetIdMapping = parentIdTargetMapping.getIdentifierMapping();
final MappingType parentIdType = parentIdTargetIdMapping instanceof CompositeIdentifierMapping ? ((CompositeIdentifierMapping) parentIdTargetIdMapping).getMappedIdEmbeddableTypeDescriptor() : parentIdTargetIdMapping.getMappedType();
if (parentIdType.getMappedJavaType().getJavaTypeClass().isInstance(event.getEntityId())) {
// yep that's what we have...
loadByDerivedIdentitySimplePkValue(event, loadType, persister, compositeIdMapping, (EntityPersister) parentIdTargetMapping);
return;
}
}
} else if (idMapping instanceof NonAggregatedIdentifierMapping) {
if (idClass.isInstance(event.getEntityId())) {
return;
}
}
}
throw new TypeMismatchException("Provided id of the wrong type for class " + persister.getEntityName() + ". Expected: " + idClass + ", got " + event.getEntityId().getClass());
}
use of org.hibernate.metamodel.mapping.CompositeIdentifierMapping in project hibernate-orm by hibernate.
the class AbstractEmbeddableInitializer method applyMapsId.
private void applyMapsId(RowProcessingState processingState) {
final SharedSessionContractImplementor session = processingState.getSession();
if (embedded instanceof CompositeIdentifierMapping) {
final CompositeIdentifierMapping cid = (CompositeIdentifierMapping) embedded;
final EmbeddableMappingType mappedIdEmbeddable = cid.getMappedIdEmbeddableTypeDescriptor();
if (cid.hasContainingClass()) {
final EmbeddableMappingType virtualIdEmbeddable = embedded.getEmbeddableTypeDescriptor();
if (virtualIdEmbeddable == mappedIdEmbeddable) {
return;
}
virtualIdEmbeddable.forEachAttributeMapping((position, virtualIdAttribute) -> {
final AttributeMapping mappedIdAttribute = mappedIdEmbeddable.getAttributeMapping(position);
if (virtualIdAttribute instanceof ToOneAttributeMapping && !(mappedIdAttribute instanceof ToOneAttributeMapping)) {
final ToOneAttributeMapping toOneAttributeMapping = (ToOneAttributeMapping) virtualIdAttribute;
final ForeignKeyDescriptor fkDescriptor = toOneAttributeMapping.getForeignKeyDescriptor();
final Object associationKey = fkDescriptor.getAssociationKeyFromSide(rowState[position], toOneAttributeMapping.getSideNature().inverse(), session);
rowState[position] = associationKey;
}
});
}
}
}
use of org.hibernate.metamodel.mapping.CompositeIdentifierMapping in project hibernate-orm by hibernate.
the class DomainResultCreationStateImpl method visitFetches.
@Override
public List<Fetch> visitFetches(FetchParent fetchParent) {
final FetchableContainer fetchableContainer = fetchParent.getReferencedMappingContainer();
final List<Fetch> fetches = CollectionHelper.arrayList(fetchableContainer.getNumberOfFetchables());
final Consumer<Fetchable> fetchableConsumer = fetchable -> {
final String fetchableName = fetchable.getFetchableName();
Map.Entry<String, NavigablePath> currentEntry;
if (relativePathStack.isEmpty()) {
currentEntry = new AbstractMap.SimpleEntry<>(getRelativePath("", fetchable, fetchableContainer), new NavigablePath(fetchableName));
} else {
final Map.Entry<String, NavigablePath> oldEntry = relativePathStack.getCurrent();
final String key = oldEntry.getKey();
currentEntry = new AbstractMap.SimpleEntry<>(getRelativePath(key, fetchable, fetchableContainer), oldEntry.getValue().append(fetchableName));
}
// todo (6.0): figure out if we can somehow create the navigable paths in a better way
final String fullPath = currentEntry.getKey();
FetchBuilder explicitFetchBuilder = fetchBuilderResolverStack.getCurrent().apply(fullPath);
DynamicFetchBuilderLegacy fetchBuilderLegacy;
if (explicitFetchBuilder == null) {
fetchBuilderLegacy = legacyFetchResolver.resolve(fromClauseAccess.findTableGroup(fetchParent.getNavigablePath()).getPrimaryTableReference().getIdentificationVariable(), fetchableName);
} else {
fetchBuilderLegacy = null;
}
if (fetchable instanceof Association && fetchable.getMappedFetchOptions().getTiming() == FetchTiming.DELAYED) {
final Association association = (Association) fetchable;
final ForeignKeyDescriptor foreignKeyDescriptor = association.getForeignKeyDescriptor();
final String partName = attributeName(foreignKeyDescriptor.getSide(association.getSideNature().inverse()).getModelPart());
// If there are no fetch builders for this association, we only want to fetch the FK
if (explicitFetchBuilder == null && fetchBuilderLegacy == null && partName != null) {
currentEntry = new AbstractMap.SimpleEntry<>(currentEntry.getKey() + "." + partName, currentEntry.getValue().append(partName));
explicitFetchBuilder = fetchBuilderResolverStack.getCurrent().apply(currentEntry.getKey());
if (explicitFetchBuilder == null) {
fetchBuilderLegacy = legacyFetchResolver.resolve(fromClauseAccess.findTableGroup(fetchParent.getNavigablePath()).getPrimaryTableReference().getIdentificationVariable(), fetchableName);
}
}
}
relativePathStack.push(currentEntry);
try {
final NavigablePath fetchPath = fetchParent.resolveNavigablePath(fetchable);
final FetchBuilder fetchBuilder;
if (explicitFetchBuilder != null) {
fetchBuilder = explicitFetchBuilder;
} else {
if (fetchBuilderLegacy == null) {
fetchBuilder = Builders.implicitFetchBuilder(fetchPath, fetchable, this);
} else {
fetchBuilder = fetchBuilderLegacy;
}
}
final Fetch fetch = fetchBuilder.buildFetch(fetchParent, fetchPath, jdbcResultsMetadata, (s, s2) -> {
throw new UnsupportedOperationException();
}, this);
fetches.add(fetch);
} finally {
relativePathStack.pop();
}
};
boolean previous = this.processingKeyFetches;
this.processingKeyFetches = true;
if (fetchableContainer instanceof EntityValuedModelPart) {
final EntityValuedModelPart entityValuedFetchable = (EntityValuedModelPart) fetchableContainer;
final EntityIdentifierMapping identifierMapping = entityValuedFetchable.getEntityMappingType().getIdentifierMapping();
final boolean idClass = identifierMapping instanceof NonAggregatedIdentifierMapping;
final String identifierAttributeName = attributeName(identifierMapping);
if (idClass) {
final Map.Entry<String, NavigablePath> oldEntry = relativePathStack.getCurrent();
relativePathStack.push(new AbstractMap.SimpleEntry<>(oldEntry == null ? "" : oldEntry.getKey(), new EntityIdentifierNavigablePath(oldEntry == null ? fetchParent.getNavigablePath() : oldEntry.getValue(), identifierAttributeName)));
} else if (identifierMapping instanceof CompositeIdentifierMapping) {
final Map.Entry<String, NavigablePath> oldEntry = relativePathStack.getCurrent();
relativePathStack.push(new AbstractMap.SimpleEntry<>(oldEntry == null ? identifierAttributeName : oldEntry.getKey() + "." + identifierAttributeName, new EntityIdentifierNavigablePath(oldEntry == null ? fetchParent.getNavigablePath() : oldEntry.getValue(), identifierAttributeName)));
}
try {
if (identifierMapping instanceof FetchableContainer) {
// essentially means the entity has a composite id - ask the embeddable to visit its fetchables
((FetchableContainer) identifierMapping).visitFetchables(fetchableConsumer, null);
} else {
fetchableConsumer.accept((Fetchable) identifierMapping);
}
} finally {
this.processingKeyFetches = previous;
if (idClass) {
this.relativePathStack.pop();
}
}
}
fetchableContainer.visitKeyFetchables(fetchableConsumer, null);
fetchableContainer.visitFetchables(fetchableConsumer, null);
return fetches;
}
Aggregations