Search in sources :

Example 1 with EntityMappingType

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());
}
Also used : MappingMetamodel(org.hibernate.metamodel.MappingMetamodel) PluralAttributeMapping(org.hibernate.metamodel.mapping.PluralAttributeMapping) AttributeMapping(org.hibernate.metamodel.mapping.AttributeMapping) EntityMappingType(org.hibernate.metamodel.mapping.EntityMappingType) Test(org.junit.jupiter.api.Test)

Example 2 with EntityMappingType

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());
}
Also used : MappingMetamodel(org.hibernate.metamodel.MappingMetamodel) PluralAttributeMapping(org.hibernate.metamodel.mapping.PluralAttributeMapping) EntityMappingType(org.hibernate.metamodel.mapping.EntityMappingType) Test(org.junit.jupiter.api.Test)

Example 3 with EntityMappingType

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);
    }
}
Also used : SortedSet(java.util.SortedSet) EntityPersister(org.hibernate.persister.entity.EntityPersister) Property(org.hibernate.mapping.Property) CompositeIdentifierMapping(org.hibernate.metamodel.mapping.CompositeIdentifierMapping) BasicType(org.hibernate.type.BasicType) CollectionIdentifierDescriptor(org.hibernate.metamodel.mapping.CollectionIdentifierDescriptor) Any(org.hibernate.mapping.Any) Joinable(org.hibernate.persister.entity.Joinable) PropertyAccess(org.hibernate.property.access.spi.PropertyAccess) ForeignKeyDirection(org.hibernate.type.ForeignKeyDirection) EntityMappingType(org.hibernate.metamodel.mapping.EntityMappingType) ToOne(org.hibernate.mapping.ToOne) NotYetImplementedFor6Exception(org.hibernate.NotYetImplementedFor6Exception) PluralAttributeMapping(org.hibernate.metamodel.mapping.PluralAttributeMapping) ImmutableMutabilityPlan(org.hibernate.type.descriptor.java.ImmutableMutabilityPlan) ForeignKeyDescriptor(org.hibernate.metamodel.mapping.ForeignKeyDescriptor) PersistentClass(org.hibernate.mapping.PersistentClass) TableGroupProducer(org.hibernate.sql.ast.tree.from.TableGroupProducer) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) CollectionPart(org.hibernate.metamodel.mapping.CollectionPart) StandardArraySemantics(org.hibernate.collection.internal.StandardArraySemantics) TypeConfiguration(org.hibernate.type.spi.TypeConfiguration) SimpleValue(org.hibernate.mapping.SimpleValue) OneToOne(org.hibernate.mapping.OneToOne) VirtualModelPart(org.hibernate.metamodel.mapping.VirtualModelPart) StringHelper(org.hibernate.internal.util.StringHelper) Value(org.hibernate.mapping.Value) ChainedPropertyAccessImpl(org.hibernate.property.access.internal.ChainedPropertyAccessImpl) Collection(org.hibernate.mapping.Collection) Serializable(java.io.Serializable) MappingMetamodel(org.hibernate.metamodel.MappingMetamodel) EntityIdentifierMapping(org.hibernate.metamodel.mapping.EntityIdentifierMapping) NavigableRole(org.hibernate.metamodel.model.domain.NavigableRole) List(java.util.List) OneToMany(org.hibernate.mapping.OneToMany) Dialect(org.hibernate.dialect.Dialect) CascadeStyle(org.hibernate.engine.spi.CascadeStyle) RuntimeModelCreationContext(org.hibernate.metamodel.spi.RuntimeModelCreationContext) MappingException(org.hibernate.MappingException) FetchMode(org.hibernate.FetchMode) SharedSessionContract(org.hibernate.SharedSessionContract) BasicValue(org.hibernate.mapping.BasicValue) CollectionPersister(org.hibernate.persister.collection.CollectionPersister) StandardIdentifierBagSemantics(org.hibernate.collection.internal.StandardIdentifierBagSemantics) CollectionMappingType(org.hibernate.metamodel.mapping.CollectionMappingType) SortedMap(java.util.SortedMap) BasicValuedModelPart(org.hibernate.metamodel.mapping.BasicValuedModelPart) MutabilityPlan(org.hibernate.type.descriptor.java.MutabilityPlan) SqlStringGenerationContext(org.hibernate.boot.model.relational.SqlStringGenerationContext) SelectableMappings(org.hibernate.metamodel.mapping.SelectableMappings) JdbcMapping(org.hibernate.metamodel.mapping.JdbcMapping) JavaType(org.hibernate.type.descriptor.java.JavaType) StandardBagSemantics(org.hibernate.collection.internal.StandardBagSemantics) SqlAliasStemHelper(org.hibernate.sql.ast.spi.SqlAliasStemHelper) EntityType(org.hibernate.type.EntityType) IndexedCollection(org.hibernate.mapping.IndexedCollection) ValueGeneration(org.hibernate.tuple.ValueGeneration) ModelPart(org.hibernate.metamodel.mapping.ModelPart) ComponentType(org.hibernate.type.ComponentType) CollectionSemantics(org.hibernate.collection.spi.CollectionSemantics) CompositeType(org.hibernate.type.CompositeType) ManagedMappingType(org.hibernate.metamodel.mapping.ManagedMappingType) SQLLoadableCollection(org.hibernate.persister.collection.SQLLoadableCollection) JavaTypeRegistry(org.hibernate.type.descriptor.java.spi.JavaTypeRegistry) FetchTiming(org.hibernate.engine.FetchTiming) ManyToOne(org.hibernate.mapping.ManyToOne) SortableValue(org.hibernate.mapping.SortableValue) Iterator(java.util.Iterator) EmbeddableMappingType(org.hibernate.metamodel.mapping.EmbeddableMappingType) ModelPartContainer(org.hibernate.metamodel.mapping.ModelPartContainer) Table(org.hibernate.mapping.Table) PropertyBasedMapping(org.hibernate.metamodel.mapping.PropertyBasedMapping) StateArrayContributorMetadataAccess(org.hibernate.metamodel.mapping.StateArrayContributorMetadataAccess) QueryableCollection(org.hibernate.persister.collection.QueryableCollection) FetchStyle(org.hibernate.engine.FetchStyle) Component(org.hibernate.mapping.Component) CollectionClassification(org.hibernate.metamodel.CollectionClassification) SelectableMapping(org.hibernate.metamodel.mapping.SelectableMapping) StateArrayContributorMetadata(org.hibernate.metamodel.mapping.StateArrayContributorMetadata) StandardListSemantics(org.hibernate.collection.internal.StandardListSemantics) Selectable(org.hibernate.mapping.Selectable) Resolvable(org.hibernate.mapping.Resolvable) EmbeddableValuedModelPart(org.hibernate.metamodel.mapping.EmbeddableValuedModelPart) BasicValueConverter(org.hibernate.metamodel.model.convert.spi.BasicValueConverter) Map(org.hibernate.mapping.Map) AssociationType(org.hibernate.type.AssociationType) Type(org.hibernate.type.Type) LOGGER(org.hibernate.metamodel.mapping.MappingModelCreationLogger.LOGGER) KeyValue(org.hibernate.mapping.KeyValue) StateArrayContributorMetadata(org.hibernate.metamodel.mapping.StateArrayContributorMetadata) Resolvable(org.hibernate.mapping.Resolvable) EmbeddableMappingType(org.hibernate.metamodel.mapping.EmbeddableMappingType) BasicValue(org.hibernate.mapping.BasicValue) FetchStyle(org.hibernate.engine.FetchStyle) ValueGeneration(org.hibernate.tuple.ValueGeneration) StateArrayContributorMetadataAccess(org.hibernate.metamodel.mapping.StateArrayContributorMetadataAccess) FetchTiming(org.hibernate.engine.FetchTiming) SimpleValue(org.hibernate.mapping.SimpleValue) Value(org.hibernate.mapping.Value) BasicValue(org.hibernate.mapping.BasicValue) SortableValue(org.hibernate.mapping.SortableValue) KeyValue(org.hibernate.mapping.KeyValue) ImmutableMutabilityPlan(org.hibernate.type.descriptor.java.ImmutableMutabilityPlan) MutabilityPlan(org.hibernate.type.descriptor.java.MutabilityPlan)

Example 4 with EntityMappingType

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);
}
Also used : EntityMappingType(org.hibernate.metamodel.mapping.EntityMappingType)

Example 5 with EntityMappingType

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);
}
Also used : BasicFetch(org.hibernate.sql.results.graph.basic.BasicFetch) TableGroup(org.hibernate.sql.ast.tree.from.TableGroup) SqlAstCreationState(org.hibernate.sql.ast.spi.SqlAstCreationState) EntityMappingType(org.hibernate.metamodel.mapping.EntityMappingType) SqlSelection(org.hibernate.sql.ast.spi.SqlSelection)

Aggregations

EntityMappingType (org.hibernate.metamodel.mapping.EntityMappingType)56 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)19 PluralAttributeMapping (org.hibernate.metamodel.mapping.PluralAttributeMapping)16 TableGroup (org.hibernate.sql.ast.tree.from.TableGroup)16 Test (org.junit.jupiter.api.Test)16 EntityIdentifierMapping (org.hibernate.metamodel.mapping.EntityIdentifierMapping)15 EntityPersister (org.hibernate.persister.entity.EntityPersister)14 AttributeMapping (org.hibernate.metamodel.mapping.AttributeMapping)11 List (java.util.List)10 ModelPart (org.hibernate.metamodel.mapping.ModelPart)10 ArrayList (java.util.ArrayList)9 MappingMetamodel (org.hibernate.metamodel.MappingMetamodel)9 ColumnReference (org.hibernate.sql.ast.tree.expression.ColumnReference)8 TableReference (org.hibernate.sql.ast.tree.from.TableReference)8 BasicValuedModelPart (org.hibernate.metamodel.mapping.BasicValuedModelPart)7 StateArrayContributorMetadata (org.hibernate.metamodel.mapping.StateArrayContributorMetadata)7 ToOneAttributeMapping (org.hibernate.metamodel.mapping.internal.ToOneAttributeMapping)7 NavigablePath (org.hibernate.query.spi.NavigablePath)7 NamedTableReference (org.hibernate.sql.ast.tree.from.NamedTableReference)7 SelectStatement (org.hibernate.sql.ast.tree.select.SelectStatement)7