Search in sources :

Example 1 with MappingMetamodel

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

the class AbstractEntityWithOneToManyTest method prepareTest.

@BeforeEach
protected void prepareTest(SessionFactoryScope scope) throws Exception {
    SessionFactoryImplementor sessionFactory = scope.getSessionFactory();
    MappingMetamodel domainModel = sessionFactory.getRuntimeMetamodels().getMappingMetamodel();
    isContractPartiesInverse = domainModel.getCollectionDescriptor(Contract.class.getName() + ".parties").isInverse();
    try {
        domainModel.getEntityDescriptor(Party.class.getName()).getPropertyType("contract");
        isContractPartiesBidirectional = true;
    } catch (QueryException ex) {
        isContractPartiesBidirectional = false;
    }
    try {
        domainModel.getEntityDescriptor(ContractVariation.class.getName()).getPropertyType("contract");
        isContractVariationsBidirectional = true;
    } catch (QueryException ex) {
        isContractVariationsBidirectional = false;
    }
    isContractVersioned = domainModel.getEntityDescriptor(Contract.class.getName()).isVersioned();
}
Also used : QueryException(org.hibernate.QueryException) MappingMetamodel(org.hibernate.metamodel.MappingMetamodel) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 2 with MappingMetamodel

use of org.hibernate.metamodel.MappingMetamodel 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 3 with MappingMetamodel

use of org.hibernate.metamodel.MappingMetamodel 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 4 with MappingMetamodel

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

the class UUIDCharTest method testUsage.

@Test
public void testUsage(SessionFactoryScope scope) {
    final MappingMetamodel domainModel = scope.getSessionFactory().getRuntimeMetamodels().getMappingMetamodel();
    final EntityPersister entityDescriptor = domainModel.findEntityDescriptor(Node.class);
    final List<JdbcMapping> identifierJdbcMappings = entityDescriptor.getIdentifierMapping().getJdbcMappings();
    assertThat(identifierJdbcMappings, hasSize(1));
    final JdbcMapping jdbcMapping = identifierJdbcMappings.get(0);
    assertThat(jdbcMapping.getJdbcType().isString(), is(true));
    final UUIDPair uuidPair = scope.fromTransaction(session -> {
        final Node root = new Node("root");
        session.save(root);
        assertThat(root.id, notNullValue());
        final Node child = new Node("child", root);
        session.save(child);
        assertThat(child.id, notNullValue());
        return new UUIDPair(root.id, child.id);
    });
    scope.inTransaction(session -> {
        final Node root = session.get(Node.class, uuidPair.rootId);
        assertThat(root, notNullValue());
        final Node child = session.get(Node.class, uuidPair.childId);
        assertThat(child, notNullValue());
    });
    scope.inTransaction(session -> {
        final Node node = session.createQuery("from Node n join fetch n.parent where n.parent is not null", Node.class).uniqueResult();
        assertThat(node, notNullValue());
        assertThat(node.parent, notNullValue());
    });
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) MappingMetamodel(org.hibernate.metamodel.MappingMetamodel) JdbcMapping(org.hibernate.metamodel.mapping.JdbcMapping) Test(org.junit.jupiter.api.Test)

Example 5 with MappingMetamodel

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

the class MetamodelBoundedCacheTest method testMemoryConsumptionOfFailedImportsCache.

@Test
@TestForIssue(jiraKey = "HHH-14948")
public void testMemoryConsumptionOfFailedImportsCache() throws NoSuchFieldException, IllegalAccessException {
    MappingMetamodel mappingMetamodel = sessionFactory().getMappingMetamodel();
    MappingMetamodelImpl mImpl = (MappingMetamodelImpl) mappingMetamodel;
    final JpaMetamodel jpaMetamodel = mImpl.getJpaMetamodel();
    for (int i = 0; i < 1001; i++) {
        jpaMetamodel.qualifyImportableName("nonexistend" + i);
    }
    Field field = JpaMetamodelImpl.class.getDeclaredField("nameToImportMap");
    field.setAccessible(true);
    // noinspection unchecked
    Map<String, String> imports = (Map<String, String>) field.get(jpaMetamodel);
    // VERY hard-coded, but considering the possibility of a regression of a memory-related issue,
    // it should be worth it
    assertEquals(1000, imports.size());
}
Also used : Field(java.lang.reflect.Field) MappingMetamodel(org.hibernate.metamodel.MappingMetamodel) MappingMetamodelImpl(org.hibernate.metamodel.model.domain.internal.MappingMetamodelImpl) JpaMetamodel(org.hibernate.metamodel.model.domain.JpaMetamodel) Map(java.util.Map) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

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