Search in sources :

Example 41 with ModelPart

use of org.hibernate.metamodel.mapping.ModelPart in project hibernate-orm by hibernate.

the class TableGroup method applySqlSelections.

@Override
default void applySqlSelections(DomainResultCreationState creationState) {
    final ModelPartContainer modelPart = getModelPart();
    final ModelPart modelPartToApply;
    if (modelPart instanceof EntityValuedModelPart) {
        modelPartToApply = ((EntityValuedModelPart) modelPart).getEntityMappingType();
    } else {
        modelPartToApply = modelPart;
    }
    modelPartToApply.applySqlSelections(getNavigablePath(), creationState.getSqlAstCreationState().getFromClauseAccess().findTableGroup(getNavigablePath()), creationState);
}
Also used : ModelPart(org.hibernate.metamodel.mapping.ModelPart) EntityValuedModelPart(org.hibernate.metamodel.mapping.EntityValuedModelPart) EntityValuedModelPart(org.hibernate.metamodel.mapping.EntityValuedModelPart) ModelPartContainer(org.hibernate.metamodel.mapping.ModelPartContainer)

Example 42 with ModelPart

use of org.hibernate.metamodel.mapping.ModelPart in project hibernate-orm by hibernate.

the class ManyToOneJoinTableTest method basicTest.

@Test
public void basicTest(SessionFactoryScope scope) {
    final EntityPersister otherDescriptor = scope.getSessionFactory().getMappingMetamodel().findEntityDescriptor(OtherEntity.class);
    final ModelPart simpleEntityAssociation = otherDescriptor.findSubPart("simpleEntity");
    assertThat(simpleEntityAssociation, instanceOf(ToOneAttributeMapping.class));
    final ToOneAttributeMapping simpleAttributeMapping = (ToOneAttributeMapping) simpleEntityAssociation;
    ForeignKeyDescriptor foreignKeyDescriptor = simpleAttributeMapping.getForeignKeyDescriptor();
    foreignKeyDescriptor.visitKeySelectables((columnIndex, selection) -> {
        assertThat(selection.getContainingTableExpression(), is("other_simple"));
        assertThat(selection.getSelectionExpression(), is("RHS_ID"));
    });
    foreignKeyDescriptor.visitTargetSelectables((columnIndex, selection) -> {
        assertThat(selection.getContainingTableExpression(), is("simple_entity"));
        assertThat(selection.getSelectionExpression(), is("id"));
    });
    final ModelPart anotherEntityAssociation = otherDescriptor.findSubPart("anotherEntity");
    assertThat(anotherEntityAssociation, instanceOf(ToOneAttributeMapping.class));
    final ToOneAttributeMapping anotherAttributeMapping = (ToOneAttributeMapping) anotherEntityAssociation;
    foreignKeyDescriptor = anotherAttributeMapping.getForeignKeyDescriptor();
    foreignKeyDescriptor.visitKeySelectables((columnIndex, selection) -> {
        assertThat(selection.getContainingTableExpression(), is("other_another"));
        assertThat(selection.getSelectionExpression(), is("RHS_ID"));
    });
    foreignKeyDescriptor.visitTargetSelectables((columnIndex, selection) -> {
        assertThat(selection.getContainingTableExpression(), is("another_entity"));
        assertThat(selection.getSelectionExpression(), is("id"));
    });
    final EntityPersister simpleDescriptor = scope.getSessionFactory().getMappingMetamodel().findEntityDescriptor(SimpleEntity.class);
    ModelPart otherEntityEntityAssociation = simpleDescriptor.findSubPart("other");
    assertThat(otherEntityEntityAssociation, instanceOf(ToOneAttributeMapping.class));
    ToOneAttributeMapping otherAttributeMapping = (ToOneAttributeMapping) otherEntityEntityAssociation;
    foreignKeyDescriptor = otherAttributeMapping.getForeignKeyDescriptor();
    foreignKeyDescriptor.visitKeySelectables((columnIndex, selection) -> {
        assertThat(selection.getContainingTableExpression(), is("other_simple"));
        assertThat(selection.getSelectionExpression(), is("LHS_ID"));
    });
    foreignKeyDescriptor.visitTargetSelectables((columnIndex, selection) -> {
        assertThat(selection.getContainingTableExpression(), is("other_entity"));
        assertThat(selection.getSelectionExpression(), is("id"));
    });
    final EntityPersister anotherDescriptor = scope.getSessionFactory().getMappingMetamodel().findEntityDescriptor(AnotherEntity.class);
    otherEntityEntityAssociation = anotherDescriptor.findSubPart("other");
    assertThat(otherEntityEntityAssociation, instanceOf(ToOneAttributeMapping.class));
    otherAttributeMapping = (ToOneAttributeMapping) otherEntityEntityAssociation;
    foreignKeyDescriptor = otherAttributeMapping.getForeignKeyDescriptor();
    foreignKeyDescriptor.visitKeySelectables((columnIndex, selection) -> {
        assertThat(selection.getContainingTableExpression(), is("another_entity"));
        assertThat(selection.getSelectionExpression(), is("other_id"));
    });
    foreignKeyDescriptor.visitTargetSelectables((columnIndex, selection) -> {
        assertThat(selection.getContainingTableExpression(), is("other_entity"));
        assertThat(selection.getSelectionExpression(), is("id"));
    });
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) ModelPart(org.hibernate.metamodel.mapping.ModelPart) ToOneAttributeMapping(org.hibernate.metamodel.mapping.internal.ToOneAttributeMapping) ForeignKeyDescriptor(org.hibernate.metamodel.mapping.ForeignKeyDescriptor) Test(org.junit.jupiter.api.Test)

Example 43 with ModelPart

use of org.hibernate.metamodel.mapping.ModelPart in project hibernate-orm by hibernate.

the class EntityWithOneToOneSharingPrimaryKeyTest method basicTest.

@Test
public void basicTest(SessionFactoryScope scope) {
    final EntityPersister entityDescriptor = scope.getSessionFactory().getMappingMetamodel().findEntityDescriptor(EntityWithOneToOneSharingPrimaryKey.class);
    final ModelPart otherAssociation = entityDescriptor.findSubPart("other");
    assertThat(otherAssociation, instanceOf(ToOneAttributeMapping.class));
    final ToOneAttributeMapping otherAttributeMapping = (ToOneAttributeMapping) otherAssociation;
    ForeignKeyDescriptor foreignKeyDescriptor = otherAttributeMapping.getForeignKeyDescriptor();
    foreignKeyDescriptor.visitKeySelectables((columnIndex, selection) -> {
        assertThat(selection.getContainingTableExpression(), is("entity_o2o_sharepk"));
        assertThat(selection.getSelectionExpression(), is("id"));
    });
    foreignKeyDescriptor.visitTargetSelectables((columnIndex, selection) -> {
        assertThat(selection.getContainingTableExpression(), is("SIMPLE_ENTITY"));
        assertThat(selection.getSelectionExpression(), is("id"));
    });
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) ModelPart(org.hibernate.metamodel.mapping.ModelPart) ToOneAttributeMapping(org.hibernate.metamodel.mapping.internal.ToOneAttributeMapping) ForeignKeyDescriptor(org.hibernate.metamodel.mapping.ForeignKeyDescriptor) Test(org.junit.jupiter.api.Test)

Example 44 with ModelPart

use of org.hibernate.metamodel.mapping.ModelPart in project hibernate-orm by hibernate.

the class NativeQueryResultBuilderTests method verifyModel.

@BeforeAll
public void verifyModel(SessionFactoryScope scope) {
    final EntityMappingType entityDescriptor = scope.getSessionFactory().getRuntimeMetamodels().getEntityMappingType(EntityOfBasics.class);
    final JdbcTypeRegistry jdbcTypeRegistry = scope.getSessionFactory().getTypeConfiguration().getJdbcTypeRegistry();
    final ModelPart part = entityDescriptor.findSubPart("convertedGender", null);
    assertThat(part, instanceOf(BasicAttributeMapping.class));
    final BasicAttributeMapping attrMapping = (BasicAttributeMapping) part;
    assertThat(attrMapping.getJavaType().getJavaTypeClass(), equalTo(EntityOfBasics.Gender.class));
    final BasicValueConverter valueConverter = attrMapping.getValueConverter();
    assertThat(valueConverter, instanceOf(JpaAttributeConverter.class));
    assertThat(valueConverter.getDomainJavaType(), is(attrMapping.getJavaType()));
    assertThat(valueConverter.getRelationalJavaType().getJavaTypeClass(), equalTo(Character.class));
    assertThat(attrMapping.getJdbcMapping().getJdbcType(), is(jdbcTypeRegistry.getDescriptor(Types.CHAR)));
}
Also used : ModelPart(org.hibernate.metamodel.mapping.ModelPart) JpaAttributeConverter(org.hibernate.metamodel.model.convert.spi.JpaAttributeConverter) BasicAttributeMapping(org.hibernate.metamodel.mapping.internal.BasicAttributeMapping) EntityMappingType(org.hibernate.metamodel.mapping.EntityMappingType) JdbcTypeRegistry(org.hibernate.type.descriptor.jdbc.spi.JdbcTypeRegistry) BasicValueConverter(org.hibernate.metamodel.model.convert.spi.BasicValueConverter) BeforeAll(org.junit.jupiter.api.BeforeAll)

Example 45 with ModelPart

use of org.hibernate.metamodel.mapping.ModelPart in project hibernate-orm by hibernate.

the class SmokeTests method testSimpleEntity.

@Test
public void testSimpleEntity(SessionFactoryScope scope) {
    final EntityPersister entityDescriptor = scope.getSessionFactory().getRuntimeMetamodels().getMappingMetamodel().getEntityDescriptor(SimpleEntity.class);
    final JdbcTypeRegistry jdbcTypeRegistry = entityDescriptor.getFactory().getTypeConfiguration().getJdbcTypeRegistry();
    final EntityIdentifierMapping identifierMapping = entityDescriptor.getIdentifierMapping();
    assertThat(identifierMapping.getMappedType().getMappedJavaType().getJavaTypeClass(), sameInstance(Integer.class));
    {
        final ModelPart namePart = entityDescriptor.findSubPart("name");
        assert namePart instanceof BasicAttributeMapping;
        assert "mapping_simple_entity".equals(((BasicAttributeMapping) namePart).getContainingTableExpression());
        assert "name".equals(((BasicAttributeMapping) namePart).getSelectionExpression());
    }
    {
        final ModelPart genderPart = entityDescriptor.findSubPart("gender");
        assert genderPart instanceof BasicAttributeMapping;
        final BasicAttributeMapping genderAttrMapping = (BasicAttributeMapping) genderPart;
        assert "mapping_simple_entity".equals(genderAttrMapping.getContainingTableExpression());
        assert "gender".equals(genderAttrMapping.getSelectionExpression());
        assertThat(genderAttrMapping.getJavaType().getJavaTypeClass(), equalTo(Gender.class));
        final BasicValueConverter valueConverter = genderAttrMapping.getValueConverter();
        assertThat(valueConverter, instanceOf(OrdinalEnumValueConverter.class));
        assertThat(valueConverter.getDomainJavaType(), is(genderAttrMapping.getJavaType()));
        assertThat(valueConverter.getRelationalJavaType().getJavaTypeClass(), equalTo(Integer.class));
        assertThat(genderAttrMapping.getJdbcMapping().getJdbcType(), is(jdbcTypeRegistry.getDescriptor(Types.SMALLINT)));
    }
    {
        final ModelPart part = entityDescriptor.findSubPart("gender2");
        assert part instanceof BasicAttributeMapping;
        final BasicAttributeMapping attrMapping = (BasicAttributeMapping) part;
        assert "mapping_simple_entity".equals(attrMapping.getContainingTableExpression());
        assert "gender2".equals(attrMapping.getSelectionExpression());
        assertThat(attrMapping.getJavaType().getJavaTypeClass(), equalTo(Gender.class));
        final BasicValueConverter valueConverter = attrMapping.getValueConverter();
        assertThat(valueConverter, instanceOf(NamedEnumValueConverter.class));
        assertThat(valueConverter.getDomainJavaType(), is(attrMapping.getJavaType()));
        assertThat(valueConverter.getRelationalJavaType().getJavaTypeClass(), equalTo(String.class));
        assertThat(attrMapping.getJdbcMapping().getJdbcType(), is(jdbcTypeRegistry.getDescriptor(Types.VARCHAR)));
    }
    {
        final ModelPart part = entityDescriptor.findSubPart("gender3");
        assert part instanceof BasicAttributeMapping;
        final BasicAttributeMapping attrMapping = (BasicAttributeMapping) part;
        assert "mapping_simple_entity".equals(attrMapping.getContainingTableExpression());
        assert "gender3".equals(attrMapping.getSelectionExpression());
        assertThat(attrMapping.getJavaType().getJavaTypeClass(), equalTo(Gender.class));
        final BasicValueConverter valueConverter = attrMapping.getValueConverter();
        assertThat(valueConverter, instanceOf(JpaAttributeConverter.class));
        assertThat(valueConverter.getDomainJavaType(), is(attrMapping.getJavaType()));
        assertThat(valueConverter.getRelationalJavaType().getJavaTypeClass(), equalTo(Character.class));
        assertThat(attrMapping.getJdbcMapping().getJdbcType(), is(jdbcTypeRegistry.getDescriptor(Types.CHAR)));
    }
    {
        final ModelPart part = entityDescriptor.findSubPart("component");
        assert part instanceof EmbeddedAttributeMapping;
        final EmbeddedAttributeMapping attrMapping = (EmbeddedAttributeMapping) part;
        assertThat(attrMapping.getContainingTableExpression(), is("mapping_simple_entity"));
        assertThat(attrMapping.getEmbeddableTypeDescriptor().getJdbcTypeCount(), is(4));
        assertThat(attrMapping.getEmbeddableTypeDescriptor().getSelectable(0).getSelectionExpression(), is("attribute1"));
        assertThat(attrMapping.getEmbeddableTypeDescriptor().getSelectable(1).getSelectionExpression(), is("attribute2"));
    }
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) ModelPart(org.hibernate.metamodel.mapping.ModelPart) EmbeddedAttributeMapping(org.hibernate.metamodel.mapping.internal.EmbeddedAttributeMapping) BasicAttributeMapping(org.hibernate.metamodel.mapping.internal.BasicAttributeMapping) EntityIdentifierMapping(org.hibernate.metamodel.mapping.EntityIdentifierMapping) JdbcTypeRegistry(org.hibernate.type.descriptor.jdbc.spi.JdbcTypeRegistry) BasicValueConverter(org.hibernate.metamodel.model.convert.spi.BasicValueConverter) Test(org.junit.jupiter.api.Test)

Aggregations

ModelPart (org.hibernate.metamodel.mapping.ModelPart)45 BasicValuedModelPart (org.hibernate.metamodel.mapping.BasicValuedModelPart)21 EmbeddableValuedModelPart (org.hibernate.metamodel.mapping.EmbeddableValuedModelPart)21 EntityValuedModelPart (org.hibernate.metamodel.mapping.EntityValuedModelPart)16 EntityPersister (org.hibernate.persister.entity.EntityPersister)15 TableGroup (org.hibernate.sql.ast.tree.from.TableGroup)15 ToOneAttributeMapping (org.hibernate.metamodel.mapping.internal.ToOneAttributeMapping)14 EntityIdentifierMapping (org.hibernate.metamodel.mapping.EntityIdentifierMapping)13 ForeignKeyDescriptor (org.hibernate.metamodel.mapping.ForeignKeyDescriptor)13 PluralAttributeMapping (org.hibernate.metamodel.mapping.PluralAttributeMapping)13 NavigablePath (org.hibernate.spi.NavigablePath)13 ArrayList (java.util.ArrayList)12 EntityMappingType (org.hibernate.metamodel.mapping.EntityMappingType)11 Test (org.junit.jupiter.api.Test)10 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)9 ColumnReference (org.hibernate.sql.ast.tree.expression.ColumnReference)9 Expression (org.hibernate.sql.ast.tree.expression.Expression)9 PluralTableGroup (org.hibernate.sql.ast.tree.from.PluralTableGroup)9 NotYetImplementedFor6Exception (org.hibernate.NotYetImplementedFor6Exception)8 ConvertibleModelPart (org.hibernate.metamodel.mapping.ConvertibleModelPart)8