use of org.hibernate.metamodel.mapping.ModelPart in project hibernate-orm by hibernate.
the class GeometryMappingTest method testSimpleEntity.
@Test
public void testSimpleEntity(SessionFactoryScope scope) {
final EntityPersister entityDescriptor = scope.getSessionFactory().getRuntimeMetamodels().getMappingMetamodel().getEntityDescriptor(MLEntity.class);
final JdbcTypeRegistry jdbcTypeRegistry = entityDescriptor.getFactory().getTypeConfiguration().getJdbcTypeRegistry();
final JavaTypeRegistry javaTypeRegistry = entityDescriptor.getFactory().getTypeConfiguration().getJavaTypeRegistry();
ModelPart part = entityDescriptor.findSubPart("lineString");
assertThat(part.getJavaType(), equalTo(GeolatteGeometryJavaType.MULTILINESTRING_INSTANCE));
}
use of org.hibernate.metamodel.mapping.ModelPart in project hibernate-orm by hibernate.
the class AbstractEntityPersister method findSubPart.
@Override
public ModelPart findSubPart(String name, EntityMappingType treatTargetType) {
LOG.tracef("#findSubPart(`%s`)", name);
if (EntityDiscriminatorMapping.matchesRoleName(name)) {
return discriminatorMapping;
}
final AttributeMapping declaredAttribute = declaredAttributeMappings.get(name);
if (declaredAttribute != null) {
return declaredAttribute;
}
if (superMappingType != null) {
final ModelPart superDefinedAttribute = superMappingType.findSubPart(name, superMappingType);
if (superDefinedAttribute != null) {
// Prefer the identifier mapping of the concrete class
if (superDefinedAttribute instanceof EntityIdentifierMapping) {
final ModelPart identifierModelPart = getIdentifierModelPart(name, treatTargetType);
if (identifierModelPart != null) {
return identifierModelPart;
}
}
return superDefinedAttribute;
}
}
if (treatTargetType != null) {
if (!treatTargetType.isTypeOrSuperType(this)) {
return null;
}
if (subclassMappingTypes != null && !subclassMappingTypes.isEmpty()) {
for (EntityMappingType subMappingType : subclassMappingTypes.values()) {
if (!treatTargetType.isTypeOrSuperType(subMappingType)) {
continue;
}
final ModelPart subDefinedAttribute = subMappingType.findSubTypesSubPart(name, treatTargetType);
if (subDefinedAttribute != null) {
return subDefinedAttribute;
}
}
}
} else {
if (subclassMappingTypes != null && !subclassMappingTypes.isEmpty()) {
ModelPart attribute = null;
for (EntityMappingType subMappingType : subclassMappingTypes.values()) {
final ModelPart subDefinedAttribute = subMappingType.findSubTypesSubPart(name, treatTargetType);
if (subDefinedAttribute != null) {
if (attribute != null && !MappingModelHelper.isCompatibleModelPart(attribute, subDefinedAttribute)) {
throw new IllegalArgumentException(new SemanticException(String.format(Locale.ROOT, "Could not resolve attribute '%s' of '%s' due to the attribute being declared in multiple sub types: ['%s', '%s']", name, getJavaType().getJavaType().getTypeName(), ((AttributeMapping) attribute).getDeclaringType().getJavaType().getJavaType().getTypeName(), ((AttributeMapping) subDefinedAttribute).getDeclaringType().getJavaType().getJavaType().getTypeName())));
}
attribute = subDefinedAttribute;
}
}
if (attribute != null) {
return attribute;
}
}
}
final ModelPart identifierModelPart = getIdentifierModelPart(name, treatTargetType);
if (identifierModelPart != null) {
return identifierModelPart;
}
for (AttributeMapping attribute : declaredAttributeMappings.values()) {
if (attribute instanceof EmbeddableValuedModelPart && attribute instanceof VirtualModelPart) {
final ModelPart subPart = ((EmbeddableValuedModelPart) attribute).findSubPart(name, null);
if (subPart != null) {
return subPart;
}
}
}
return null;
}
use of org.hibernate.metamodel.mapping.ModelPart in project hibernate-orm by hibernate.
the class CompleteResultBuilderCollectionStandard method resolveSelections.
private void resolveSelections(TableGroup tableGroup, ModelPart modelPart, String[] columnNames, JdbcValuesMetadata jdbcResultsMetadata, DomainResultCreationStateImpl creationStateImpl) {
final SelectableConsumer consumer = (selectionIndex, selectableMapping) -> {
final String columnName = columnNames[selectionIndex];
creationStateImpl.resolveSqlSelection(creationStateImpl.resolveSqlExpression(SqlExpressionResolver.createColumnReferenceKey(tableGroup.resolveTableReference(selectableMapping.getContainingTableExpression()), selectableMapping.getSelectionExpression()), processingState -> {
final int jdbcPosition = jdbcResultsMetadata.resolveColumnPosition(columnName);
final BasicValuedMapping basicType = (BasicValuedMapping) selectableMapping.getJdbcMapping();
final int valuesArrayPosition = ResultsHelper.jdbcPositionToValuesArrayPosition(jdbcPosition);
return new ResultSetMappingSqlSelection(valuesArrayPosition, basicType);
}), selectableMapping.getJdbcMapping().getMappedJavaType(), null, creationStateImpl.getSessionFactory().getTypeConfiguration());
};
if (modelPart instanceof EntityValuedModelPart) {
final EntityMappingType entityMappingType = ((EntityValuedModelPart) modelPart).getEntityMappingType();
int index = entityMappingType.getIdentifierMapping().forEachSelectable(consumer);
if (entityMappingType.getDiscriminatorMapping() != null) {
index += entityMappingType.getDiscriminatorMapping().forEachSelectable(index, consumer);
}
entityMappingType.forEachSelectable(index, consumer);
} else {
modelPart.forEachSelectable(consumer);
}
}
use of org.hibernate.metamodel.mapping.ModelPart in project hibernate-orm by hibernate.
the class ResultsHelper method createRowReader.
public static <R> RowReader<R> createRowReader(ExecutionContext executionContext, LockOptions lockOptions, RowTransformer<R> rowTransformer, Class<R> transformedResultJavaType, JdbcValues jdbcValues) {
final SessionFactoryImplementor sessionFactory = executionContext.getSession().getFactory();
final Map<NavigablePath, Initializer> initializerMap = new LinkedHashMap<>();
final List<Initializer> initializers = new ArrayList<>();
final List<DomainResultAssembler<?>> assemblers = jdbcValues.getValuesMapping().resolveAssemblers(new AssemblerCreationState() {
@Override
public LockMode determineEffectiveLockMode(String identificationVariable) {
return lockOptions.getEffectiveLockMode(identificationVariable);
}
@Override
public Initializer resolveInitializer(NavigablePath navigablePath, ModelPart fetchedModelPart, Supplier<Initializer> producer) {
final Initializer existing = initializerMap.get(navigablePath);
if (existing != null) {
if (fetchedModelPart.getNavigableRole().equals(existing.getInitializedPart().getNavigableRole())) {
ResultsLogger.RESULTS_MESSAGE_LOGGER.tracef("Returning previously-registered initializer : %s", existing);
return existing;
}
}
final Initializer initializer = producer.get();
ResultsLogger.RESULTS_MESSAGE_LOGGER.tracef("Registering initializer : %s", initializer);
initializerMap.put(navigablePath, initializer);
initializers.add(initializer);
return initializer;
}
@Override
public SqlAstCreationContext getSqlAstCreationContext() {
return sessionFactory;
}
});
logInitializers(initializerMap);
return new StandardRowReader<>(assemblers, initializers, rowTransformer, transformedResultJavaType);
}
use of org.hibernate.metamodel.mapping.ModelPart in project hibernate-orm by hibernate.
the class InverseNonAggregatedIdentifierMapping method getIdentifier.
@Override
public Object getIdentifier(Object entity) {
if (hasContainingClass()) {
final Object id = identifierValueMapper.getRepresentationStrategy().getInstantiator().instantiate(null, // sessionFactory
null);
final List<AttributeMapping> attributeMappings = getEmbeddableTypeDescriptor().getAttributeMappings();
final List<AttributeMapping> idClassAttributeMappings = identifierValueMapper.getAttributeMappings();
final Object[] propertyValues = new Object[attributeMappings.size()];
for (int i = 0; i < propertyValues.length; i++) {
final AttributeMapping attributeMapping = attributeMappings.get(i);
final Object o = attributeMapping.getPropertyAccess().getGetter().get(entity);
if (o == null) {
final AttributeMapping idClassAttributeMapping = idClassAttributeMappings.get(i);
if (idClassAttributeMapping.getPropertyAccess().getGetter().getReturnTypeClass().isPrimitive()) {
propertyValues[i] = idClassAttributeMapping.getExpressibleJavaType().getDefaultValue();
} else {
propertyValues[i] = null;
}
} else // JPA 2 @MapsId + @IdClass points to the pk of the entity
if (attributeMapping instanceof ToOneAttributeMapping && !(idClassAttributeMappings.get(i) instanceof ToOneAttributeMapping)) {
final ToOneAttributeMapping toOneAttributeMapping = (ToOneAttributeMapping) attributeMapping;
final ModelPart targetPart = toOneAttributeMapping.getForeignKeyDescriptor().getPart(toOneAttributeMapping.getSideNature().inverse());
if (targetPart instanceof EntityIdentifierMapping) {
propertyValues[i] = ((EntityIdentifierMapping) targetPart).getIdentifier(o);
} else {
propertyValues[i] = o;
assert false;
}
} else {
propertyValues[i] = o;
}
}
identifierValueMapper.setValues(id, propertyValues);
return id;
} else {
return entity;
}
}
Aggregations