use of org.hibernate.metamodel.mapping.AttributeMetadata in project hibernate-orm by hibernate.
the class AbstractEntityPersister method setPropertyValue.
@Override
public void setPropertyValue(Object object, String propertyName, Object value) {
final AttributeMapping attributeMapping = (AttributeMapping) findSubPart(propertyName, this);
final AttributeMetadata attributeMetadata = attributeMapping.getAttributeMetadataAccess().resolveAttributeMetadata(this);
attributeMetadata.getPropertyAccess().getSetter().set(object, value);
}
use of org.hibernate.metamodel.mapping.AttributeMetadata in project hibernate-orm by hibernate.
the class AbstractCompositeIdAndNaturalIdTest method testNaturalIdNullability.
@Test
@TestForIssue(jiraKey = "HHH-10360")
public void testNaturalIdNullability(SessionFactoryScope scope) {
final EntityMappingType accountMapping = scope.getSessionFactory().getRuntimeMetamodels().getEntityMappingType(Account.class);
final SingularAttributeMapping shortCodeMapping = ((SimpleNaturalIdMapping) accountMapping.getNaturalIdMapping()).getAttribute();
final AttributeMetadata shortCodeMetadata = shortCodeMapping.getAttributeMetadataAccess().resolveAttributeMetadata(null);
assertThat(shortCodeMetadata.isNullable(), is(false));
final EntityPersister rootEntityPersister = accountMapping.getRootEntityDescriptor().getEntityPersister();
final int shortCodeLegacyPropertyIndex = rootEntityPersister.getEntityMetamodel().getPropertyIndex("shortCode");
assertThat(shortCodeLegacyPropertyIndex, is(0));
assertThat(rootEntityPersister.getPropertyNullability()[shortCodeLegacyPropertyIndex], is(false));
}
use of org.hibernate.metamodel.mapping.AttributeMetadata in project hibernate-orm by hibernate.
the class ImmutableManyToOneNaturalIdAnnotationTest method testNaturalIdNullability.
@Test
@TestForIssue(jiraKey = "HHH-10360")
public void testNaturalIdNullability(SessionFactoryScope scope) {
// nullability is not specified for either properties making up
// the natural ID, so they should be nullable by annotation-specific default
final RuntimeMetamodels runtimeMetamodels = scope.getSessionFactory().getRuntimeMetamodels();
final EntityMappingType childMapping = runtimeMetamodels.getEntityMappingType(Child.class.getName());
final EntityPersister persister = childMapping.getEntityPersister();
final EntityMetamodel entityMetamodel = persister.getEntityMetamodel();
final int nameIndex = entityMetamodel.getPropertyIndex("name");
final int parentIndex = entityMetamodel.getPropertyIndex("parent");
// checking alphabetic sort in relation to EntityPersister/EntityMetamodel
assertThat(nameIndex, lessThan(parentIndex));
assertFalse(persister.getPropertyUpdateability()[nameIndex]);
assertFalse(persister.getPropertyUpdateability()[parentIndex]);
assertTrue(persister.getPropertyNullability()[nameIndex]);
assertTrue(persister.getPropertyNullability()[parentIndex]);
final NaturalIdMapping naturalIdMapping = childMapping.getNaturalIdMapping();
assertNotNull(naturalIdMapping);
assertThat(naturalIdMapping.getNaturalIdAttributes().size(), is(2));
// access by list-index should again be alphabetically sorted
final SingularAttributeMapping first = naturalIdMapping.getNaturalIdAttributes().get(0);
assertThat(first.getAttributeName(), is("name"));
final AttributeMetadata firstMetadata = first.getAttributeMetadataAccess().resolveAttributeMetadata(null);
assertFalse(firstMetadata.getMutabilityPlan().isMutable());
final SingularAttributeMapping second = naturalIdMapping.getNaturalIdAttributes().get(1);
assertThat(second.getAttributeName(), is("parent"));
final AttributeMetadata secondMetadata = second.getAttributeMetadataAccess().resolveAttributeMetadata(null);
assertFalse(secondMetadata.getMutabilityPlan().isMutable());
}
use of org.hibernate.metamodel.mapping.AttributeMetadata in project hibernate-orm by hibernate.
the class MappingModelCreationHelper method buildBasicAttributeMapping.
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Non-identifier attributes
@SuppressWarnings("rawtypes")
public static BasicAttributeMapping buildBasicAttributeMapping(String attrName, NavigableRole navigableRole, int stateArrayPosition, Property bootProperty, ManagedMappingType declaringType, BasicType attrType, String tableExpression, String attrColumnName, boolean isAttrFormula, String readExpr, String writeExpr, String columnDefinition, Long length, Integer precision, Integer scale, PropertyAccess propertyAccess, CascadeStyle cascadeStyle, MappingModelCreationProcess creationProcess) {
final Value value = bootProperty.getValue();
final BasicValue.Resolution<?> resolution = ((Resolvable) value).resolve();
final BasicValueConverter<?, ?> valueConverter = resolution.getValueConverter();
final AttributeMetadataAccess attributeMetadataAccess = entityMappingType -> new AttributeMetadata() {
private final MutabilityPlan mutabilityPlan = resolution.getMutabilityPlan();
private final boolean nullable = value.isNullable();
private final boolean insertable = bootProperty.isInsertable();
private final boolean updateable = bootProperty.isUpdateable();
private final boolean includeInOptimisticLocking = bootProperty.isOptimisticLocked();
@Override
public PropertyAccess getPropertyAccess() {
return propertyAccess;
}
@Override
public MutabilityPlan getMutabilityPlan() {
return mutabilityPlan;
}
@Override
public boolean isNullable() {
return nullable;
}
@Override
public boolean isInsertable() {
return insertable;
}
@Override
public boolean isUpdatable() {
return updateable;
}
@Override
public boolean isIncludedInDirtyChecking() {
// todo (6.0) : do not believe this is correct
return updateable;
}
@Override
public boolean isIncludedInOptimisticLocking() {
return includeInOptimisticLocking;
}
@Override
public CascadeStyle getCascadeStyle() {
return cascadeStyle;
}
};
final FetchTiming fetchTiming;
final FetchStyle fetchStyle;
if (declaringType instanceof EmbeddableMappingType) {
if (bootProperty.isLazy()) {
LOGGER.debugf("Attribute was declared lazy, but is part of an embeddable - `%s#%s` - LAZY will be ignored", declaringType.getNavigableRole().getFullPath(), bootProperty.getName());
}
fetchTiming = FetchTiming.IMMEDIATE;
fetchStyle = FetchStyle.JOIN;
} else {
fetchTiming = bootProperty.isLazy() ? FetchTiming.DELAYED : FetchTiming.IMMEDIATE;
fetchStyle = bootProperty.isLazy() ? FetchStyle.SELECT : FetchStyle.JOIN;
}
final ValueGeneration valueGeneration = bootProperty.getValueGenerationStrategy();
if (valueConverter != null) {
// we want to "decompose" the "type" into its various pieces as expected by the mapping
assert valueConverter.getRelationalJavaType() == resolution.getRelationalJavaType();
final BasicType<?> mappingBasicType = creationProcess.getCreationContext().getDomainModel().getTypeConfiguration().getBasicTypeRegistry().resolve(valueConverter.getRelationalJavaType(), resolution.getJdbcType());
return new BasicAttributeMapping(attrName, navigableRole, stateArrayPosition, attributeMetadataAccess, fetchTiming, fetchStyle, tableExpression, attrColumnName, isAttrFormula, null, null, columnDefinition, length, precision, scale, valueConverter, mappingBasicType.getJdbcMapping(), declaringType, propertyAccess, valueGeneration);
} else {
return new BasicAttributeMapping(attrName, navigableRole, stateArrayPosition, attributeMetadataAccess, fetchTiming, fetchStyle, tableExpression, attrColumnName, isAttrFormula, readExpr, writeExpr, columnDefinition, length, precision, scale, null, attrType, declaringType, propertyAccess, valueGeneration);
}
}
use of org.hibernate.metamodel.mapping.AttributeMetadata in project hibernate-orm by hibernate.
the class AbstractEntityPersister method generateNonIdAttributeMapping.
private AttributeMapping generateNonIdAttributeMapping(NonIdentifierAttribute tupleAttrDefinition, Property bootProperty, int stateArrayPosition, MappingModelCreationProcess creationProcess) {
final SessionFactoryImplementor sessionFactory = creationProcess.getCreationContext().getSessionFactory();
final JdbcServices jdbcServices = sessionFactory.getJdbcServices();
final JdbcEnvironment jdbcEnvironment = jdbcServices.getJdbcEnvironment();
final Dialect dialect = jdbcEnvironment.getDialect();
final String attrName = tupleAttrDefinition.getName();
final Type attrType = tupleAttrDefinition.getType();
final int propertyIndex = getPropertyIndex(bootProperty.getName());
final String tableExpression = getTableName(getPropertyTableNumbers()[propertyIndex]);
final String[] attrColumnNames = getPropertyColumnNames(propertyIndex);
final PropertyAccess propertyAccess = getRepresentationStrategy().resolvePropertyAccess(bootProperty);
if (propertyIndex == getVersionProperty()) {
Column column = bootProperty.getValue().getColumns().get(0);
return MappingModelCreationHelper.buildBasicAttributeMapping(attrName, getNavigableRole().append(bootProperty.getName()), stateArrayPosition, bootProperty, this, (BasicType<?>) attrType, tableExpression, attrColumnNames[0], false, null, null, column.getSqlType(), column.getLength(), column.getPrecision(), column.getScale(), propertyAccess, tupleAttrDefinition.getCascadeStyle(), creationProcess);
}
if (attrType instanceof BasicType) {
final Value bootValue = bootProperty.getValue();
final String attrColumnExpression;
final boolean isAttrColumnExpressionFormula;
final String customReadExpr;
final String customWriteExpr;
final String columnDefinition;
final Long length;
final Integer precision;
final Integer scale;
if (bootValue instanceof DependantValue) {
attrColumnExpression = attrColumnNames[0];
isAttrColumnExpressionFormula = false;
customReadExpr = null;
customWriteExpr = null;
Column column = bootValue.getColumns().get(0);
columnDefinition = column.getSqlType();
length = column.getLength();
precision = column.getPrecision();
scale = column.getScale();
} else {
final BasicValue basicBootValue = (BasicValue) bootValue;
if (attrColumnNames[0] != null) {
attrColumnExpression = attrColumnNames[0];
isAttrColumnExpressionFormula = false;
final List<Selectable> selectables = basicBootValue.getSelectables();
assert !selectables.isEmpty();
final Selectable selectable = selectables.get(0);
assert attrColumnExpression.equals(selectable.getText(sessionFactory.getJdbcServices().getDialect()));
customReadExpr = selectable.getTemplate(dialect, sessionFactory.getTypeConfiguration(), sessionFactory.getQueryEngine().getSqmFunctionRegistry());
customWriteExpr = selectable.getCustomWriteExpression();
Column column = bootValue.getColumns().get(0);
columnDefinition = column.getSqlType();
length = column.getLength();
precision = column.getPrecision();
scale = column.getScale();
} else {
final String[] attrColumnFormulaTemplate = propertyColumnFormulaTemplates[propertyIndex];
attrColumnExpression = attrColumnFormulaTemplate[0];
isAttrColumnExpressionFormula = true;
customReadExpr = null;
customWriteExpr = null;
columnDefinition = null;
length = null;
precision = null;
scale = null;
}
}
return MappingModelCreationHelper.buildBasicAttributeMapping(attrName, getNavigableRole().append(bootProperty.getName()), stateArrayPosition, bootProperty, this, (BasicType<?>) attrType, tableExpression, attrColumnExpression, isAttrColumnExpressionFormula, customReadExpr, customWriteExpr, columnDefinition, length, precision, scale, propertyAccess, tupleAttrDefinition.getCascadeStyle(), creationProcess);
} else if (attrType instanceof AnyType) {
final JavaType<Object> baseAssociationJtd = sessionFactory.getTypeConfiguration().getJavaTypeRegistry().getDescriptor(Object.class);
final AnyType anyType = (AnyType) attrType;
return new DiscriminatedAssociationAttributeMapping(navigableRole.append(bootProperty.getName()), baseAssociationJtd, this, stateArrayPosition, entityMappingType -> new AttributeMetadata() {
private final MutabilityPlan<?> mutabilityPlan = new DiscriminatedAssociationAttributeMapping.MutabilityPlanImpl(anyType);
private final boolean nullable = bootProperty.isOptional();
private final boolean insertable = bootProperty.isInsertable();
private final boolean updateable = bootProperty.isUpdateable();
private final boolean optimisticallyLocked = bootProperty.isOptimisticLocked();
@Override
public PropertyAccess getPropertyAccess() {
return propertyAccess;
}
@Override
public MutabilityPlan<?> getMutabilityPlan() {
return mutabilityPlan;
}
@Override
public boolean isNullable() {
return nullable;
}
@Override
public boolean isInsertable() {
return insertable;
}
@Override
public boolean isUpdatable() {
return updateable;
}
@Override
public boolean isIncludedInDirtyChecking() {
return updateable;
}
@Override
public boolean isIncludedInOptimisticLocking() {
return optimisticallyLocked;
}
}, bootProperty.isLazy() ? FetchTiming.DELAYED : FetchTiming.IMMEDIATE, propertyAccess, bootProperty, (AnyType) attrType, (Any) bootProperty.getValue(), creationProcess);
} else if (attrType instanceof CompositeType) {
return MappingModelCreationHelper.buildEmbeddedAttributeMapping(attrName, stateArrayPosition, bootProperty, this, (CompositeType) attrType, tableExpression, null, propertyAccess, tupleAttrDefinition.getCascadeStyle(), creationProcess);
} else if (attrType instanceof CollectionType) {
return MappingModelCreationHelper.buildPluralAttributeMapping(attrName, stateArrayPosition, bootProperty, this, propertyAccess, tupleAttrDefinition.getCascadeStyle(), getFetchMode(stateArrayPosition), creationProcess);
} else if (attrType instanceof EntityType) {
return MappingModelCreationHelper.buildSingularAssociationAttributeMapping(attrName, getNavigableRole().append(attrName), stateArrayPosition, bootProperty, this, this, (EntityType) attrType, propertyAccess, tupleAttrDefinition.getCascadeStyle(), creationProcess);
}
return null;
}
Aggregations