Search in sources :

Example 1 with RuntimeMetamodels

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

the class EntityHidingTests method testModel.

@Test
@NotImplementedYet(reason = "Contributed entity hiding is not yet implemented", strict = false)
public void testModel(SessionFactoryScope scope) {
    final SessionFactoryImplementor sessionFactory = scope.getSessionFactory();
    final RuntimeMetamodels runtimeMetamodels = sessionFactory.getRuntimeMetamodels();
    final EntityDomainType<Object> jpaModelDescriptor = runtimeMetamodels.getJpaMetamodel().entity("DynamicEntity");
    assertThat(jpaModelDescriptor, nullValue());
    final EntityPersister mappingModelDescriptor = runtimeMetamodels.getMappingMetamodel().findEntityDescriptor("DynamicEntity");
    assertThat(mappingModelDescriptor, nullValue());
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) RuntimeMetamodels(org.hibernate.metamodel.RuntimeMetamodels) NotImplementedYet(org.hibernate.testing.orm.junit.NotImplementedYet) Test(org.junit.jupiter.api.Test)

Example 2 with RuntimeMetamodels

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

the class AbstractManagedType method isCompatible.

private boolean isCompatible(PersistentAttribute<?, ?> attribute1, PersistentAttribute<?, ?> attribute2) {
    if (attribute1 == attribute2) {
        return true;
    }
    final RuntimeMetamodels runtimeMetamodels = jpaMetamodel().getTypeConfiguration().getSessionFactory().getRuntimeMetamodels();
    final EntityMappingType entity1 = runtimeMetamodels.getEntityMappingType(attribute1.getDeclaringType().getTypeName());
    final EntityMappingType entity2 = runtimeMetamodels.getEntityMappingType(attribute2.getDeclaringType().getTypeName());
    return entity1 != null && entity2 != null && MappingModelHelper.isCompatibleModelPart(entity1.findSubPart(attribute1.getName()), entity2.findSubPart(attribute2.getName()));
}
Also used : RuntimeMetamodels(org.hibernate.metamodel.RuntimeMetamodels) EntityMappingType(org.hibernate.metamodel.mapping.EntityMappingType)

Example 3 with RuntimeMetamodels

use of org.hibernate.metamodel.RuntimeMetamodels 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 StateArrayContributorMetadata firstMetadata = first.getAttributeMetadataAccess().resolveAttributeMetadata(null);
    assertFalse(firstMetadata.getMutabilityPlan().isMutable());
    final SingularAttributeMapping second = naturalIdMapping.getNaturalIdAttributes().get(1);
    assertThat(second.getAttributeName(), is("parent"));
    final StateArrayContributorMetadata secondMetadata = second.getAttributeMetadataAccess().resolveAttributeMetadata(null);
    assertFalse(secondMetadata.getMutabilityPlan().isMutable());
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) NaturalIdMapping(org.hibernate.metamodel.mapping.NaturalIdMapping) RuntimeMetamodels(org.hibernate.metamodel.RuntimeMetamodels) SingularAttributeMapping(org.hibernate.metamodel.mapping.SingularAttributeMapping) StateArrayContributorMetadata(org.hibernate.metamodel.mapping.StateArrayContributorMetadata) EntityMappingType(org.hibernate.metamodel.mapping.EntityMappingType) EntityMetamodel(org.hibernate.tuple.entity.EntityMetamodel) Test(org.junit.jupiter.api.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 4 with RuntimeMetamodels

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

the class Builders method attributeResult.

public static ResultBuilder attributeResult(String columnAlias, String entityName, String attributePath, SessionFactoryImplementor sessionFactory) {
    if (attributePath.contains(".")) {
        throw new NotYetImplementedFor6Exception("Support for defining a NativeQuery attribute result based on a composite path is not yet implemented");
    }
    final RuntimeMetamodels runtimeMetamodels = sessionFactory.getRuntimeMetamodels();
    final String fullEntityName = runtimeMetamodels.getMappingMetamodel().getImportedName(entityName);
    final EntityPersister entityMapping = runtimeMetamodels.getMappingMetamodel().findEntityDescriptor(fullEntityName);
    if (entityMapping == null) {
        throw new IllegalArgumentException("Could not locate entity mapping : " + fullEntityName);
    }
    final AttributeMapping attributeMapping = entityMapping.findAttributeMapping(attributePath);
    if (attributeMapping == null) {
        throw new IllegalArgumentException("Could not locate attribute mapping : " + fullEntityName + "." + attributePath);
    }
    if (attributeMapping instanceof SingularAttributeMapping) {
        final SingularAttributeMapping singularAttributeMapping = (SingularAttributeMapping) attributeMapping;
        return new DynamicResultBuilderAttribute(singularAttributeMapping, columnAlias, fullEntityName, attributePath);
    }
    throw new IllegalArgumentException(String.format(Locale.ROOT, "Specified attribute mapping [%s.%s] not a basic attribute: %s", fullEntityName, attributePath, attributeMapping));
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) DynamicResultBuilderAttribute(org.hibernate.query.results.dynamic.DynamicResultBuilderAttribute) PluralAttributeMapping(org.hibernate.metamodel.mapping.PluralAttributeMapping) ToOneAttributeMapping(org.hibernate.metamodel.mapping.internal.ToOneAttributeMapping) SingularAttributeMapping(org.hibernate.metamodel.mapping.SingularAttributeMapping) AttributeMapping(org.hibernate.metamodel.mapping.AttributeMapping) NotYetImplementedFor6Exception(org.hibernate.NotYetImplementedFor6Exception) RuntimeMetamodels(org.hibernate.metamodel.RuntimeMetamodels) SingularAttributeMapping(org.hibernate.metamodel.mapping.SingularAttributeMapping)

Example 5 with RuntimeMetamodels

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

the class Builders method entityCalculated.

/**
 * Creates a EntityResultBuilder that does not allow any further configuring of the mapping.
 *
 * @see #entityCalculated(String, String, SessionFactoryImplementor)
 * @see NativeQuery#addEntity(String, Class, LockMode)
 * @see NativeQuery#addEntity(String, String, LockMode)
 */
public static DynamicResultBuilderEntityCalculated entityCalculated(String tableAlias, String entityName, LockMode explicitLockMode, SessionFactoryImplementor sessionFactory) {
    final RuntimeMetamodels runtimeMetamodels = sessionFactory.getRuntimeMetamodels();
    final EntityMappingType entityMapping = runtimeMetamodels.getEntityMappingType(entityName);
    return new DynamicResultBuilderEntityCalculated(entityMapping, tableAlias, explicitLockMode, sessionFactory);
}
Also used : RuntimeMetamodels(org.hibernate.metamodel.RuntimeMetamodels) EntityMappingType(org.hibernate.metamodel.mapping.EntityMappingType) DynamicResultBuilderEntityCalculated(org.hibernate.query.results.dynamic.DynamicResultBuilderEntityCalculated)

Aggregations

RuntimeMetamodels (org.hibernate.metamodel.RuntimeMetamodels)13 Test (org.junit.jupiter.api.Test)8 EntityPersister (org.hibernate.persister.entity.EntityPersister)7 EntityMappingType (org.hibernate.metamodel.mapping.EntityMappingType)6 EmbeddableMappingType (org.hibernate.metamodel.mapping.EmbeddableMappingType)3 SingularAttributeMapping (org.hibernate.metamodel.mapping.SingularAttributeMapping)3 EmbeddedAttributeMapping (org.hibernate.metamodel.mapping.internal.EmbeddedAttributeMapping)3 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)2 MappingMetamodel (org.hibernate.metamodel.MappingMetamodel)2 AttributeMapping (org.hibernate.metamodel.mapping.AttributeMapping)2 NaturalIdMapping (org.hibernate.metamodel.mapping.NaturalIdMapping)2 PluralAttributeMapping (org.hibernate.metamodel.mapping.PluralAttributeMapping)2 StateArrayContributorMetadata (org.hibernate.metamodel.mapping.StateArrayContributorMetadata)2 EmbeddableRepresentationStrategy (org.hibernate.metamodel.spi.EmbeddableRepresentationStrategy)2 TestForIssue (org.hibernate.testing.TestForIssue)2 EntityMetamodel (org.hibernate.tuple.entity.EntityMetamodel)2 NotYetImplementedFor6Exception (org.hibernate.NotYetImplementedFor6Exception)1 RootGraphImpl (org.hibernate.graph.internal.RootGraphImpl)1 EmbeddedCollectionPart (org.hibernate.metamodel.mapping.internal.EmbeddedCollectionPart)1 ToOneAttributeMapping (org.hibernate.metamodel.mapping.internal.ToOneAttributeMapping)1