use of org.hibernate.metamodel.mapping.EntityMappingType in project hibernate-orm by hibernate.
the class PluralAttributeMappingTests method testLists.
@Test
public void testLists(SessionFactoryScope scope) {
final MappingMetamodel domainModel = scope.getSessionFactory().getRuntimeMetamodels().getMappingMetamodel();
final EntityMappingType containerEntityDescriptor = domainModel.getEntityDescriptor(EntityOfLists.class);
assertThat(containerEntityDescriptor.getNumberOfAttributeMappings(), is(8));
final AttributeMapping listOfBasics = containerEntityDescriptor.findAttributeMapping("listOfBasics");
assertThat(listOfBasics, notNullValue());
final AttributeMapping listOfEnums = containerEntityDescriptor.findAttributeMapping("listOfEnums");
assertThat(listOfEnums, notNullValue());
final AttributeMapping listOfConvertedBasics = containerEntityDescriptor.findAttributeMapping("listOfConvertedEnums");
assertThat(listOfConvertedBasics, notNullValue());
final AttributeMapping listOfComponents = containerEntityDescriptor.findAttributeMapping("listOfComponents");
assertThat(listOfComponents, notNullValue());
final AttributeMapping listOfOneToMany = containerEntityDescriptor.findAttributeMapping("listOfOneToMany");
assertThat(listOfOneToMany, notNullValue());
final AttributeMapping listOfManyToMany = containerEntityDescriptor.findAttributeMapping("listOfManyToMany");
assertThat(listOfManyToMany, notNullValue());
}
use of org.hibernate.metamodel.mapping.EntityMappingType in project hibernate-orm by hibernate.
the class PluralAttributeMappingTests method testMaps.
@Test
public void testMaps(SessionFactoryScope scope) {
final MappingMetamodel domainModel = scope.getSessionFactory().getRuntimeMetamodels().getMappingMetamodel();
final EntityMappingType containerEntityDescriptor = domainModel.getEntityDescriptor(EntityOfMaps.class);
assertThat(containerEntityDescriptor.getNumberOfAttributeMappings(), is(17));
final PluralAttributeMapping basicByBasic = (PluralAttributeMapping) containerEntityDescriptor.findAttributeMapping("basicByBasic");
assertThat(basicByBasic, notNullValue());
assertThat(basicByBasic.getKeyDescriptor(), notNullValue());
assertThat(basicByBasic.getElementDescriptor(), notNullValue());
final PluralAttributeMapping sortedBasicByBasic = (PluralAttributeMapping) containerEntityDescriptor.findAttributeMapping("sortedBasicByBasic");
assertThat(sortedBasicByBasic, notNullValue());
assertThat(sortedBasicByBasic.getKeyDescriptor(), notNullValue());
assertThat(sortedBasicByBasic.getElementDescriptor(), notNullValue());
final PluralAttributeMapping sortedBasicByBasicWithSortNaturalByDefault = (PluralAttributeMapping) containerEntityDescriptor.findAttributeMapping("sortedBasicByBasicWithSortNaturalByDefault");
assertThat(sortedBasicByBasicWithSortNaturalByDefault, notNullValue());
assertThat(sortedBasicByBasicWithSortNaturalByDefault.getKeyDescriptor(), notNullValue());
assertThat(sortedBasicByBasicWithSortNaturalByDefault.getElementDescriptor(), notNullValue());
final PluralAttributeMapping basicByEnum = (PluralAttributeMapping) containerEntityDescriptor.findAttributeMapping("basicByEnum");
assertThat(basicByEnum, notNullValue());
assertThat(basicByEnum.getKeyDescriptor(), notNullValue());
assertThat(basicByEnum.getElementDescriptor(), notNullValue());
final PluralAttributeMapping basicByConvertedEnum = (PluralAttributeMapping) containerEntityDescriptor.findAttributeMapping("basicByConvertedEnum");
assertThat(basicByConvertedEnum, notNullValue());
assertThat(basicByConvertedEnum.getKeyDescriptor(), notNullValue());
assertThat(basicByConvertedEnum.getElementDescriptor(), notNullValue());
final PluralAttributeMapping someStuffByBasic = (PluralAttributeMapping) containerEntityDescriptor.findAttributeMapping("componentByBasic");
assertThat(someStuffByBasic, notNullValue());
assertThat(someStuffByBasic.getKeyDescriptor(), notNullValue());
assertThat(someStuffByBasic.getElementDescriptor(), notNullValue());
final PluralAttributeMapping basicBySomeStuff = (PluralAttributeMapping) containerEntityDescriptor.findAttributeMapping("basicByComponent");
assertThat(basicBySomeStuff, notNullValue());
assertThat(basicBySomeStuff.getKeyDescriptor(), notNullValue());
assertThat(basicBySomeStuff.getElementDescriptor(), notNullValue());
final PluralAttributeMapping oneToManyByBasic = (PluralAttributeMapping) containerEntityDescriptor.findAttributeMapping("oneToManyByBasic");
assertThat(oneToManyByBasic, notNullValue());
assertThat(oneToManyByBasic.getKeyDescriptor(), notNullValue());
assertThat(oneToManyByBasic.getElementDescriptor(), notNullValue());
final PluralAttributeMapping basicByOneToMany = (PluralAttributeMapping) containerEntityDescriptor.findAttributeMapping("basicByOneToMany");
assertThat(basicByOneToMany, notNullValue());
assertThat(basicByOneToMany.getKeyDescriptor(), notNullValue());
assertThat(basicByOneToMany.getElementDescriptor(), notNullValue());
final PluralAttributeMapping manyToManyByBasic = (PluralAttributeMapping) containerEntityDescriptor.findAttributeMapping("manyToManyByBasic");
assertThat(manyToManyByBasic, notNullValue());
assertThat(manyToManyByBasic.getKeyDescriptor(), notNullValue());
assertThat(manyToManyByBasic.getElementDescriptor(), notNullValue());
final PluralAttributeMapping sortedManyToManyByBasic = (PluralAttributeMapping) containerEntityDescriptor.findAttributeMapping("sortedManyToManyByBasic");
assertThat(sortedManyToManyByBasic, notNullValue());
assertThat(sortedManyToManyByBasic.getKeyDescriptor(), notNullValue());
assertThat(sortedManyToManyByBasic.getElementDescriptor(), notNullValue());
final PluralAttributeMapping sortedManyToManyByBasicWithSortNaturalByDefault = (PluralAttributeMapping) containerEntityDescriptor.findAttributeMapping("sortedManyToManyByBasicWithSortNaturalByDefault");
assertThat(sortedManyToManyByBasicWithSortNaturalByDefault, notNullValue());
assertThat(sortedManyToManyByBasicWithSortNaturalByDefault.getKeyDescriptor(), notNullValue());
assertThat(sortedManyToManyByBasicWithSortNaturalByDefault.getElementDescriptor(), notNullValue());
final PluralAttributeMapping componentByBasicOrdered = (PluralAttributeMapping) containerEntityDescriptor.findAttributeMapping("componentByBasicOrdered");
assertThat(componentByBasicOrdered, notNullValue());
assertThat(componentByBasicOrdered.getKeyDescriptor(), notNullValue());
assertThat(componentByBasicOrdered.getElementDescriptor(), notNullValue());
assertThat(componentByBasicOrdered.getOrderByFragment(), notNullValue());
}
use of org.hibernate.metamodel.mapping.EntityMappingType 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 StateArrayContributorMetadataAccess attributeMetadataAccess = entityMappingType -> new StateArrayContributorMetadata() {
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.EntityMappingType in project hibernate-orm by hibernate.
the class NaturalIdResolutionsImpl method cacheResolutionLocally.
/**
* Private, but see {@link #cacheResolution} for public version
*/
private boolean cacheResolutionLocally(Object id, Object naturalId, EntityMappingType entityDescriptor) {
// by the time we get here we assume that the natural-id value has already been validated so just do an assert
assert entityDescriptor.getNaturalIdMapping() != null;
assert isValidValue(naturalId, entityDescriptor);
NaturalIdLogging.LOGGER.debugf("Locally caching natural-id resolution (%s) : `%s` -> `%s`", entityDescriptor.getEntityName(), naturalId, id);
final EntityMappingType rootEntityDescriptor = entityDescriptor.getRootEntityDescriptor();
final EntityResolutions previousEntry = resolutionsByEntity.get(rootEntityDescriptor);
final EntityResolutions resolutions;
if (previousEntry != null) {
resolutions = previousEntry;
} else {
resolutions = new EntityResolutions(rootEntityDescriptor, persistenceContext);
resolutionsByEntity.put(rootEntityDescriptor, resolutions);
}
return resolutions.cache(id, naturalId);
}
use of org.hibernate.metamodel.mapping.EntityMappingType in project hibernate-orm by hibernate.
the class BasicAttributeMapping method generateFetch.
@Override
public Fetch generateFetch(FetchParent fetchParent, NavigablePath fetchablePath, FetchTiming fetchTiming, boolean selected, String resultVariable, DomainResultCreationState creationState) {
final int valuesArrayPosition;
// Lazy property. A valuesArrayPosition of -1 will lead to
// returning a domain result assembler that returns LazyPropertyInitializer.UNFETCHED_PROPERTY
final EntityMappingType containingEntityMapping = findContainingEntityMapping();
if (fetchTiming == FetchTiming.DELAYED && containingEntityMapping.getEntityPersister().getPropertyLaziness()[getStateArrayPosition()]) {
valuesArrayPosition = -1;
} else {
final SqlAstCreationState sqlAstCreationState = creationState.getSqlAstCreationState();
final TableGroup tableGroup = sqlAstCreationState.getFromClauseAccess().getTableGroup(fetchParent.getNavigablePath());
assert tableGroup != null;
final SqlSelection sqlSelection = resolveSqlSelection(fetchablePath, tableGroup, true, creationState);
valuesArrayPosition = sqlSelection.getValuesArrayPosition();
}
return new BasicFetch<>(valuesArrayPosition, fetchParent, fetchablePath, this, valueConverter, fetchTiming, creationState);
}
Aggregations