Search in sources :

Example 21 with MappingMetamodel

use of org.hibernate.metamodel.MappingMetamodel in project hibernate-orm by hibernate.

the class PluralAttributeMappingTests method testSets.

@Test
public void testSets(SessionFactoryScope scope) {
    final MappingMetamodel domainModel = scope.getSessionFactory().getRuntimeMetamodels().getMappingMetamodel();
    final EntityMappingType containerEntityDescriptor = domainModel.getEntityDescriptor(EntityOfSets.class);
    assertThat(containerEntityDescriptor.getNumberOfAttributeMappings(), is(12));
    final AttributeMapping setOfBasics = containerEntityDescriptor.findAttributeMapping("setOfBasics");
    assertThat(setOfBasics, notNullValue());
    final AttributeMapping sortedSetOfBasics = containerEntityDescriptor.findAttributeMapping("sortedSetOfBasics");
    assertThat(sortedSetOfBasics, notNullValue());
    final AttributeMapping sortedSetOfBasicsWithSortNaturalByDefault = containerEntityDescriptor.findAttributeMapping("sortedSetOfBasicsWithSortNaturalByDefault");
    assertThat(sortedSetOfBasicsWithSortNaturalByDefault, notNullValue());
    final AttributeMapping orderedSetOfBasics = containerEntityDescriptor.findAttributeMapping("orderedSetOfBasics");
    assertThat(orderedSetOfBasics, notNullValue());
    final AttributeMapping setOfEnums = containerEntityDescriptor.findAttributeMapping("setOfEnums");
    assertThat(setOfEnums, notNullValue());
    final AttributeMapping setOfConvertedBasics = containerEntityDescriptor.findAttributeMapping("setOfConvertedEnums");
    assertThat(setOfConvertedBasics, notNullValue());
    final AttributeMapping setOfComponents = containerEntityDescriptor.findAttributeMapping("setOfComponents");
    assertThat(setOfComponents, notNullValue());
    final AttributeMapping extraLazySetOfComponents = containerEntityDescriptor.findAttributeMapping("extraLazySetOfComponents");
    assertThat(extraLazySetOfComponents, notNullValue());
    final AttributeMapping setOfOneToMany = containerEntityDescriptor.findAttributeMapping("setOfOneToMany");
    assertThat(setOfOneToMany, notNullValue());
    final AttributeMapping setOfManyToMany = containerEntityDescriptor.findAttributeMapping("setOfManyToMany");
    assertThat(setOfManyToMany, 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 22 with MappingMetamodel

use of org.hibernate.metamodel.MappingMetamodel in project hibernate-orm by hibernate.

the class InstantiationTests method runtimeModelTest.

@Test
public void runtimeModelTest(SessionFactoryScope scope) {
    final RuntimeMetamodels runtimeMetamodels = scope.getSessionFactory().getRuntimeMetamodels();
    final MappingMetamodel mappingMetamodel = runtimeMetamodels.getMappingMetamodel();
    final EntityPersister entityDescriptor = mappingMetamodel.findEntityDescriptor(Person.class);
    final EmbeddedAttributeMapping nameEmbedded = (EmbeddedAttributeMapping) entityDescriptor.findAttributeMapping("name");
    final EmbeddableMappingType nameEmbeddable = nameEmbedded.getEmbeddableTypeDescriptor();
    final EmbeddableRepresentationStrategy nameRepStrategy = nameEmbeddable.getRepresentationStrategy();
    assertThat(nameRepStrategy.getMode()).isEqualTo(RepresentationMode.POJO);
    assertThat(nameRepStrategy.getInstantiator()).isInstanceOf(NameInstantiator.class);
    nameEmbeddable.forEachAttributeMapping((position, attribute) -> {
        assertThat(attribute.getPropertyAccess().getSetter()).isNull();
    });
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) MappingMetamodel(org.hibernate.metamodel.MappingMetamodel) EmbeddableRepresentationStrategy(org.hibernate.metamodel.spi.EmbeddableRepresentationStrategy) EmbeddedAttributeMapping(org.hibernate.metamodel.mapping.internal.EmbeddedAttributeMapping) RuntimeMetamodels(org.hibernate.metamodel.RuntimeMetamodels) EmbeddableMappingType(org.hibernate.metamodel.mapping.EmbeddableMappingType) Test(org.junit.jupiter.api.Test)

Example 23 with MappingMetamodel

use of org.hibernate.metamodel.MappingMetamodel in project hibernate-orm by hibernate.

the class InstantiationTests method runtimeModelTest.

@Test
public void runtimeModelTest(SessionFactoryScope scope) {
    final RuntimeMetamodels runtimeMetamodels = scope.getSessionFactory().getRuntimeMetamodels();
    final MappingMetamodel mappingMetamodel = runtimeMetamodels.getMappingMetamodel();
    final EntityPersister entityDescriptor = mappingMetamodel.findEntityDescriptor(Person.class);
    final EmbeddedAttributeMapping nameEmbedded = (EmbeddedAttributeMapping) entityDescriptor.findAttributeMapping("name");
    final EmbeddableMappingType nameEmbeddable = nameEmbedded.getEmbeddableTypeDescriptor();
    final EmbeddableRepresentationStrategy nameRepStrategy = nameEmbeddable.getRepresentationStrategy();
    assertThat(nameRepStrategy.getMode()).isEqualTo(RepresentationMode.POJO);
    assertThat(nameRepStrategy.getInstantiator()).isInstanceOf(NameInstantiator.class);
    nameEmbeddable.forEachAttributeMapping((position, attribute) -> {
        assertThat(attribute.getPropertyAccess().getSetter()).isNull();
    });
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) MappingMetamodel(org.hibernate.metamodel.MappingMetamodel) EmbeddableRepresentationStrategy(org.hibernate.metamodel.spi.EmbeddableRepresentationStrategy) EmbeddedAttributeMapping(org.hibernate.metamodel.mapping.internal.EmbeddedAttributeMapping) RuntimeMetamodels(org.hibernate.metamodel.RuntimeMetamodels) EmbeddableMappingType(org.hibernate.metamodel.mapping.EmbeddableMappingType) Test(org.junit.jupiter.api.Test)

Example 24 with MappingMetamodel

use of org.hibernate.metamodel.MappingMetamodel in project hibernate-orm by hibernate.

the class CompoundNaturalIdTests method testProcessing.

@Test
public void testProcessing(DomainModelScope domainModelScope, SessionFactoryScope factoryScope) {
    final PersistentClass accountBootMapping = domainModelScope.getDomainModel().getEntityBinding(Account.class.getName());
    assertThat(accountBootMapping.hasNaturalId(), is(true));
    final Property username = accountBootMapping.getProperty("username");
    assertThat(username.isNaturalIdentifier(), is(true));
    final Property system = accountBootMapping.getProperty("system");
    assertThat(system.isNaturalIdentifier(), is(true));
    final MappingMetamodel mappingMetamodel = factoryScope.getSessionFactory().getRuntimeMetamodels().getMappingMetamodel();
    final EntityPersister accountMapping = mappingMetamodel.findEntityDescriptor(Account.class);
    assertThat(accountMapping.hasNaturalIdentifier(), is(true));
    final NaturalIdMapping naturalIdMapping = accountMapping.getNaturalIdMapping();
    assertThat(naturalIdMapping, notNullValue());
    final List<SingularAttributeMapping> attributes = naturalIdMapping.getNaturalIdAttributes();
    assertThat(attributes.size(), is(2));
    // alphabetical matching overall processing
    final SingularAttributeMapping first = attributes.get(0);
    assertThat(first, notNullValue());
    assertThat(first.getAttributeName(), is("system"));
    final SingularAttributeMapping second = attributes.get(1);
    assertThat(second, notNullValue());
    assertThat(second.getAttributeName(), is("username"));
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) MappingMetamodel(org.hibernate.metamodel.MappingMetamodel) NaturalIdMapping(org.hibernate.metamodel.mapping.NaturalIdMapping) SingularAttributeMapping(org.hibernate.metamodel.mapping.SingularAttributeMapping) Property(org.hibernate.mapping.Property) PersistentClass(org.hibernate.mapping.PersistentClass) Test(org.junit.jupiter.api.Test)

Example 25 with MappingMetamodel

use of org.hibernate.metamodel.MappingMetamodel in project hibernate-orm by hibernate.

the class MappedFetchTests method baseline.

@Test
public void baseline(SessionFactoryScope scope) {
    final SessionFactoryImplementor sessionFactory = scope.getSessionFactory();
    final MappingMetamodel domainModel = sessionFactory.getRuntimeMetamodels().getMappingMetamodel();
    final EntityPersister rootEntityDescriptor = domainModel.getEntityDescriptor(RootEntity.class);
    final SelectStatement sqlAst = LoaderSelectBuilder.createSelect(rootEntityDescriptor, null, rootEntityDescriptor.getIdentifierMapping(), null, 1, LoadQueryInfluencers.NONE, LockOptions.NONE, jdbcParameter -> {
    }, sessionFactory);
    assertThat(sqlAst.getDomainResultDescriptors().size(), is(1));
    final DomainResult domainResult = sqlAst.getDomainResultDescriptors().get(0);
    assertThat(domainResult, instanceOf(EntityResult.class));
    final EntityResult entityResult = (EntityResult) domainResult;
    final List<Fetch> fetches = entityResult.getFetches();
    // name + both lists
    assertThat(fetches.size(), is(3));
    // order is alphabetical...
    final Fetch nameFetch = fetches.get(0);
    assertThat(nameFetch.getFetchedMapping().getFetchableName(), is("name"));
    assertThat(nameFetch, instanceOf(BasicFetch.class));
    final Fetch nickNamesFetch = fetches.get(1);
    assertThat(nickNamesFetch.getFetchedMapping().getFetchableName(), is("nickNames"));
    assertThat(nickNamesFetch, instanceOf(EagerCollectionFetch.class));
    final Fetch simpleEntitiesFetch = fetches.get(2);
    assertThat(simpleEntitiesFetch.getFetchedMapping().getFetchableName(), is("simpleEntities"));
    assertThat(simpleEntitiesFetch, instanceOf(DelayedCollectionFetch.class));
    final QuerySpec querySpec = sqlAst.getQuerySpec();
    final TableGroup tableGroup = querySpec.getFromClause().getRoots().get(0);
    assertThat(tableGroup.getModelPart(), is(rootEntityDescriptor));
    assertThat(tableGroup.getTableGroupJoins().size(), is(1));
    final TableGroupJoin collectionJoin = tableGroup.getTableGroupJoins().iterator().next();
    assertThat(collectionJoin.getJoinedGroup().getModelPart(), is(nickNamesFetch.getFetchedMapping()));
    assertThat(collectionJoin.getPredicate(), notNullValue());
    assertThat(collectionJoin.getPredicate(), instanceOf(ComparisonPredicate.class));
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) BasicFetch(org.hibernate.sql.results.graph.basic.BasicFetch) TableGroup(org.hibernate.sql.ast.tree.from.TableGroup) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) DomainResult(org.hibernate.sql.results.graph.DomainResult) EntityResult(org.hibernate.sql.results.graph.entity.EntityResult) ComparisonPredicate(org.hibernate.sql.ast.tree.predicate.ComparisonPredicate) DelayedCollectionFetch(org.hibernate.sql.results.graph.collection.internal.DelayedCollectionFetch) BasicFetch(org.hibernate.sql.results.graph.basic.BasicFetch) Fetch(org.hibernate.sql.results.graph.Fetch) EagerCollectionFetch(org.hibernate.sql.results.graph.collection.internal.EagerCollectionFetch) TableGroupJoin(org.hibernate.sql.ast.tree.from.TableGroupJoin) SelectStatement(org.hibernate.sql.ast.tree.select.SelectStatement) DelayedCollectionFetch(org.hibernate.sql.results.graph.collection.internal.DelayedCollectionFetch) MappingMetamodel(org.hibernate.metamodel.MappingMetamodel) QuerySpec(org.hibernate.sql.ast.tree.select.QuerySpec) EagerCollectionFetch(org.hibernate.sql.results.graph.collection.internal.EagerCollectionFetch) Test(org.junit.jupiter.api.Test)

Aggregations

MappingMetamodel (org.hibernate.metamodel.MappingMetamodel)25 Test (org.junit.jupiter.api.Test)13 EntityPersister (org.hibernate.persister.entity.EntityPersister)12 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)7 EntityMappingType (org.hibernate.metamodel.mapping.EntityMappingType)7 PluralAttributeMapping (org.hibernate.metamodel.mapping.PluralAttributeMapping)5 TableGroup (org.hibernate.sql.ast.tree.from.TableGroup)5 ArrayList (java.util.ArrayList)4 List (java.util.List)4 Map (java.util.Map)4 PersistentClass (org.hibernate.mapping.PersistentClass)4 AttributeMapping (org.hibernate.metamodel.mapping.AttributeMapping)4 EntityDomainType (org.hibernate.metamodel.model.domain.EntityDomainType)4 QueryException (org.hibernate.QueryException)3 EmbeddableMappingType (org.hibernate.metamodel.mapping.EmbeddableMappingType)3 JdbcMapping (org.hibernate.metamodel.mapping.JdbcMapping)3 MappingModelExpressible (org.hibernate.metamodel.mapping.MappingModelExpressible)3 NaturalIdMapping (org.hibernate.metamodel.mapping.NaturalIdMapping)3 DiscriminatorSqmPath (org.hibernate.metamodel.model.domain.internal.DiscriminatorSqmPath)3 Joinable (org.hibernate.persister.entity.Joinable)3