Search in sources :

Example 36 with EntityIdentifierMapping

use of org.hibernate.metamodel.mapping.EntityIdentifierMapping 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.TINYINT)));
    }
    {
        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

EntityIdentifierMapping (org.hibernate.metamodel.mapping.EntityIdentifierMapping)36 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)14 EntityMappingType (org.hibernate.metamodel.mapping.EntityMappingType)14 AttributeMapping (org.hibernate.metamodel.mapping.AttributeMapping)13 ModelPart (org.hibernate.metamodel.mapping.ModelPart)13 ArrayList (java.util.ArrayList)12 PluralAttributeMapping (org.hibernate.metamodel.mapping.PluralAttributeMapping)12 TableGroup (org.hibernate.sql.ast.tree.from.TableGroup)12 EntityPersister (org.hibernate.persister.entity.EntityPersister)11 ColumnReference (org.hibernate.sql.ast.tree.expression.ColumnReference)10 Expression (org.hibernate.sql.ast.tree.expression.Expression)10 ForeignKeyDescriptor (org.hibernate.metamodel.mapping.ForeignKeyDescriptor)9 NavigablePath (org.hibernate.query.spi.NavigablePath)9 List (java.util.List)8 BasicValuedModelPart (org.hibernate.metamodel.mapping.BasicValuedModelPart)8 TableReference (org.hibernate.sql.ast.tree.from.TableReference)8 AbstractMap (java.util.AbstractMap)6 Collections (java.util.Collections)6 Map (java.util.Map)6 Consumer (java.util.function.Consumer)6