Search in sources :

Example 6 with RuntimeMetamodels

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

the class AbstractCommonQueryContract method applyGraph.

protected void applyGraph(String graphString, GraphSemantic graphSemantic) {
    final int separatorPosition = graphString.indexOf('(');
    final int terminatorPosition = graphString.lastIndexOf(')');
    if (separatorPosition < 0 || terminatorPosition < 0) {
        throw new IllegalArgumentException(String.format(ROOT, "Invalid entity-graph definition `%s`; expected form `${EntityName}( ${property1} ... )", graphString));
    }
    final RuntimeMetamodels runtimeMetamodels = getSession().getFactory().getRuntimeMetamodels();
    final JpaMetamodel jpaMetamodel = runtimeMetamodels.getJpaMetamodel();
    final String entityName = runtimeMetamodels.getImportedName(graphString.substring(0, separatorPosition).trim());
    final String graphNodes = graphString.substring(separatorPosition + 1, terminatorPosition);
    final RootGraphImpl<?> rootGraph = new RootGraphImpl<>(null, jpaMetamodel.entity(entityName), jpaMetamodel);
    GraphParser.parseInto((EntityGraph<?>) rootGraph, graphNodes, getSession().getSessionFactory());
    applyGraph(rootGraph, graphSemantic);
}
Also used : RuntimeMetamodels(org.hibernate.metamodel.RuntimeMetamodels) RootGraphImpl(org.hibernate.graph.internal.RootGraphImpl) JpaMetamodel(org.hibernate.metamodel.model.domain.JpaMetamodel)

Example 7 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)

Example 8 with RuntimeMetamodels

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

the class Builders method entity.

/**
 * Creates a EntityResultBuilder allowing for further configuring of the mapping.
 */
public static DynamicResultBuilderEntityStandard entity(String tableAlias, String entityName, SessionFactoryImplementor sessionFactory) {
    final RuntimeMetamodels runtimeMetamodels = sessionFactory.getRuntimeMetamodels();
    final EntityMappingType entityMapping = runtimeMetamodels.getEntityMappingType(entityName);
    return new DynamicResultBuilderEntityStandard(entityMapping, tableAlias);
}
Also used : RuntimeMetamodels(org.hibernate.metamodel.RuntimeMetamodels) EntityMappingType(org.hibernate.metamodel.mapping.EntityMappingType) DynamicResultBuilderEntityStandard(org.hibernate.query.results.dynamic.DynamicResultBuilderEntityStandard)

Example 9 with RuntimeMetamodels

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

the class ImmutableManyToOneNaturalIdHbmTest method checkingMapping.

@Test
@TestForIssue(jiraKey = "HHH-10360")
public void checkingMapping(SessionFactoryScope scope) {
    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]);
    // nullability is not specified for either properties making up
    // the natural ID, so they should be non-nullable by hbm-specific default
    assertFalse(persister.getPropertyNullability()[nameIndex]);
    assertFalse(persister.getPropertyNullability()[parentIndex]);
    final NaturalIdMapping naturalIdMapping = childMapping.getNaturalIdMapping();
    assertNotNull(naturalIdMapping);
    assertThat(naturalIdMapping.getNaturalIdAttributes().size(), is(2));
    final SingularAttributeMapping first = naturalIdMapping.getNaturalIdAttributes().get(0);
    assertThat(first.getAttributeName(), is("name"));
    final AttributeMetadata firstMetadata = first.getAttributeMetadataAccess().resolveAttributeMetadata(null);
    assertFalse(firstMetadata.getMutabilityPlan().isMutable());
    final SingularAttributeMapping second = naturalIdMapping.getNaturalIdAttributes().get(1);
    assertThat(second.getAttributeName(), is("parent"));
    final AttributeMetadata secondMetadata = second.getAttributeMetadataAccess().resolveAttributeMetadata(null);
    assertFalse(secondMetadata.getMutabilityPlan().isMutable());
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) AttributeMetadata(org.hibernate.metamodel.mapping.AttributeMetadata) NaturalIdMapping(org.hibernate.metamodel.mapping.NaturalIdMapping) RuntimeMetamodels(org.hibernate.metamodel.RuntimeMetamodels) SingularAttributeMapping(org.hibernate.metamodel.mapping.SingularAttributeMapping) EntityMappingType(org.hibernate.metamodel.mapping.EntityMappingType) EntityMetamodel(org.hibernate.tuple.entity.EntityMetamodel) Test(org.junit.jupiter.api.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 10 with RuntimeMetamodels

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

the class EmbeddableAccessTests method verifyRuntimeModel.

@Test
public void verifyRuntimeModel(SessionFactoryScope scope) {
    final RuntimeMetamodels runtimeMetamodels = scope.getSessionFactory().getRuntimeMetamodels();
    final EntityMappingType personDescriptor = runtimeMetamodels.getEntityMappingType(Person.class);
    // Person defines FIELD access, while Name uses PROPERTY
    // - if we find the property annotations, the attribute names will be
    // `firstName` and `lastName`, and the columns `first_name` and `last_name`
    // - otherwise, we have property and column names being `first` and `last`
    final EmbeddableMappingType nameEmbeddable = ((EmbeddedAttributeMapping) personDescriptor.findAttributeMapping("name")).getEmbeddableTypeDescriptor();
    assertThat(nameEmbeddable.getNumberOfAttributeMappings()).isEqualTo(2);
    final AttributeMapping nameFirst = nameEmbeddable.getAttributeMapping(0);
    final AttributeMapping nameLast = nameEmbeddable.getAttributeMapping(1);
    assertThat(nameFirst.getAttributeName()).isEqualTo("firstName");
    assertThat(nameLast.getAttributeName()).isEqualTo("lastName");
    final PluralAttributeMapping aliasesAttribute = (PluralAttributeMapping) personDescriptor.findAttributeMapping("aliases");
    final EmbeddableMappingType aliasEmbeddable = ((EmbeddedCollectionPart) aliasesAttribute.getElementDescriptor()).getEmbeddableTypeDescriptor();
    assertThat(aliasEmbeddable.getNumberOfAttributeMappings()).isEqualTo(2);
    final AttributeMapping aliasFirst = nameEmbeddable.getAttributeMapping(0);
    final AttributeMapping aliasLast = nameEmbeddable.getAttributeMapping(1);
    assertThat(aliasFirst.getAttributeName()).isEqualTo("firstName");
    assertThat(aliasLast.getAttributeName()).isEqualTo("lastName");
}
Also used : EmbeddedAttributeMapping(org.hibernate.metamodel.mapping.internal.EmbeddedAttributeMapping) EmbeddedCollectionPart(org.hibernate.metamodel.mapping.internal.EmbeddedCollectionPart) PluralAttributeMapping(org.hibernate.metamodel.mapping.PluralAttributeMapping) AttributeMapping(org.hibernate.metamodel.mapping.AttributeMapping) EmbeddedAttributeMapping(org.hibernate.metamodel.mapping.internal.EmbeddedAttributeMapping) PluralAttributeMapping(org.hibernate.metamodel.mapping.PluralAttributeMapping) RuntimeMetamodels(org.hibernate.metamodel.RuntimeMetamodels) EmbeddableMappingType(org.hibernate.metamodel.mapping.EmbeddableMappingType) EntityMappingType(org.hibernate.metamodel.mapping.EntityMappingType) Test(org.junit.jupiter.api.Test)

Aggregations

RuntimeMetamodels (org.hibernate.metamodel.RuntimeMetamodels)14 Test (org.junit.jupiter.api.Test)9 EntityMappingType (org.hibernate.metamodel.mapping.EntityMappingType)7 EntityPersister (org.hibernate.persister.entity.EntityPersister)7 EmbeddableMappingType (org.hibernate.metamodel.mapping.EmbeddableMappingType)4 EmbeddedAttributeMapping (org.hibernate.metamodel.mapping.internal.EmbeddedAttributeMapping)4 AttributeMapping (org.hibernate.metamodel.mapping.AttributeMapping)3 PluralAttributeMapping (org.hibernate.metamodel.mapping.PluralAttributeMapping)3 SingularAttributeMapping (org.hibernate.metamodel.mapping.SingularAttributeMapping)3 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)2 MappingMetamodel (org.hibernate.metamodel.MappingMetamodel)2 AttributeMetadata (org.hibernate.metamodel.mapping.AttributeMetadata)2 NaturalIdMapping (org.hibernate.metamodel.mapping.NaturalIdMapping)2 EmbeddedCollectionPart (org.hibernate.metamodel.mapping.internal.EmbeddedCollectionPart)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 ToOneAttributeMapping (org.hibernate.metamodel.mapping.internal.ToOneAttributeMapping)1