Search in sources :

Example 36 with BasicAttributeMapping

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

the class StringMappingTests method testMappings.

@Test
public void testMappings(SessionFactoryScope scope) {
    // first, verify the type selections...
    final MappingMetamodelImplementor mappingMetamodel = scope.getSessionFactory().getRuntimeMetamodels().getMappingMetamodel();
    final JdbcTypeRegistry jdbcRegistry = mappingMetamodel.getTypeConfiguration().getJdbcTypeRegistry();
    final EntityPersister entityDescriptor = mappingMetamodel.findEntityDescriptor(EntityOfStrings.class);
    {
        final BasicAttributeMapping attribute = (BasicAttributeMapping) entityDescriptor.findAttributeMapping("string");
        assertThat(attribute.getJavaType().getJavaTypeClass(), equalTo(String.class));
        final JdbcMapping jdbcMapping = attribute.getJdbcMapping();
        assertThat(jdbcMapping.getJavaTypeDescriptor().getJavaTypeClass(), equalTo(String.class));
        assertThat(jdbcMapping.getJdbcType(), equalTo(jdbcRegistry.getDescriptor(Types.VARCHAR)));
    }
    {
        final BasicAttributeMapping attribute = (BasicAttributeMapping) entityDescriptor.findAttributeMapping("clobString");
        final JdbcMapping jdbcMapping = attribute.getJdbcMapping();
        assertThat(jdbcMapping.getJavaTypeDescriptor().getJavaTypeClass(), equalTo(String.class));
        assertThat(jdbcMapping.getJdbcType(), equalTo(jdbcRegistry.getDescriptor(Types.CLOB)));
    }
    // and try to use the mapping
    scope.inTransaction((session) -> session.persist(new EntityOfStrings(1, "string", "clob")));
    scope.inTransaction((session) -> session.get(EntityOfStrings.class, 1));
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) JdbcMapping(org.hibernate.metamodel.mapping.JdbcMapping) BasicAttributeMapping(org.hibernate.metamodel.mapping.internal.BasicAttributeMapping) MappingMetamodelImplementor(org.hibernate.metamodel.spi.MappingMetamodelImplementor) JdbcTypeRegistry(org.hibernate.type.descriptor.jdbc.spi.JdbcTypeRegistry) Test(org.junit.jupiter.api.Test)

Example 37 with BasicAttributeMapping

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

the class IntegerMappingTests method testMappings.

@Test
public void testMappings(SessionFactoryScope scope) {
    // first, verify the type selections...
    final MappingMetamodelImplementor mappingMetamodel = scope.getSessionFactory().getRuntimeMetamodels().getMappingMetamodel();
    final EntityPersister entityDescriptor = mappingMetamodel.findEntityDescriptor(EntityOfIntegers.class);
    {
        final BasicAttributeMapping attribute = (BasicAttributeMapping) entityDescriptor.findAttributeMapping("wrapper");
        assertThat(attribute.getJavaType().getJavaTypeClass(), equalTo(Integer.class));
        final JdbcMapping jdbcMapping = attribute.getJdbcMapping();
        assertThat(jdbcMapping.getJavaTypeDescriptor().getJavaTypeClass(), equalTo(Integer.class));
        assertThat(jdbcMapping.getJdbcType().getJdbcTypeCode(), is(Types.INTEGER));
    }
    {
        final BasicAttributeMapping attribute = (BasicAttributeMapping) entityDescriptor.findAttributeMapping("primitive");
        assertThat(attribute.getJavaType().getJavaTypeClass(), equalTo(Integer.class));
        final JdbcMapping jdbcMapping = attribute.getJdbcMapping();
        assertThat(jdbcMapping.getJavaTypeDescriptor().getJavaTypeClass(), equalTo(Integer.class));
        assertThat(jdbcMapping.getJdbcType().getJdbcTypeCode(), is(Types.INTEGER));
    }
    // and try to use the mapping
    scope.inTransaction((session) -> session.persist(new EntityOfIntegers(1, 3, 5)));
    scope.inTransaction((session) -> session.get(EntityOfIntegers.class, 1));
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) JdbcMapping(org.hibernate.metamodel.mapping.JdbcMapping) BasicAttributeMapping(org.hibernate.metamodel.mapping.internal.BasicAttributeMapping) MappingMetamodelImplementor(org.hibernate.metamodel.spi.MappingMetamodelImplementor) Test(org.junit.jupiter.api.Test)

Example 38 with BasicAttributeMapping

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

the class LocalDateMappingTests method verifyMappings.

@Test
public void verifyMappings(SessionFactoryScope scope) {
    final MappingMetamodelImplementor mappingMetamodel = scope.getSessionFactory().getRuntimeMetamodels().getMappingMetamodel();
    final EntityPersister entityDescriptor = mappingMetamodel.findEntityDescriptor(EntityWithLocalDate.class);
    final BasicAttributeMapping duration = (BasicAttributeMapping) entityDescriptor.findAttributeMapping("localDate");
    final JdbcMapping jdbcMapping = duration.getJdbcMapping();
    assertThat(jdbcMapping.getJavaTypeDescriptor().getJavaTypeClass(), equalTo(LocalDate.class));
    assertThat(jdbcMapping.getJdbcType().getJdbcTypeCode(), equalTo(Types.DATE));
    scope.inTransaction((session) -> {
        session.persist(new EntityWithLocalDate(1, LocalDate.now()));
    });
    scope.inTransaction((session) -> session.find(EntityWithLocalDate.class, 1));
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) JdbcMapping(org.hibernate.metamodel.mapping.JdbcMapping) BasicAttributeMapping(org.hibernate.metamodel.mapping.internal.BasicAttributeMapping) MappingMetamodelImplementor(org.hibernate.metamodel.spi.MappingMetamodelImplementor) LocalDate(java.time.LocalDate) Test(org.junit.jupiter.api.Test)

Example 39 with BasicAttributeMapping

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

the class LocalTimeMappingTests method verifyMappings.

@Test
public void verifyMappings(SessionFactoryScope scope) {
    final MappingMetamodelImplementor mappingMetamodel = scope.getSessionFactory().getRuntimeMetamodels().getMappingMetamodel();
    final EntityPersister entityDescriptor = mappingMetamodel.findEntityDescriptor(EntityWithLocalTime.class);
    final BasicAttributeMapping duration = (BasicAttributeMapping) entityDescriptor.findAttributeMapping("localTime");
    final JdbcMapping jdbcMapping = duration.getJdbcMapping();
    assertThat(jdbcMapping.getJavaTypeDescriptor().getJavaTypeClass(), equalTo(LocalTime.class));
    assertThat(jdbcMapping.getJdbcType().getJdbcTypeCode(), equalTo(Types.TIME));
    scope.inTransaction((session) -> {
        session.persist(new EntityWithLocalTime(1, LocalTime.now()));
    });
    scope.inTransaction((session) -> session.find(EntityWithLocalTime.class, 1));
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) JdbcMapping(org.hibernate.metamodel.mapping.JdbcMapping) LocalTime(java.time.LocalTime) BasicAttributeMapping(org.hibernate.metamodel.mapping.internal.BasicAttributeMapping) MappingMetamodelImplementor(org.hibernate.metamodel.spi.MappingMetamodelImplementor) Test(org.junit.jupiter.api.Test)

Example 40 with BasicAttributeMapping

use of org.hibernate.metamodel.mapping.internal.BasicAttributeMapping 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

BasicAttributeMapping (org.hibernate.metamodel.mapping.internal.BasicAttributeMapping)41 Test (org.junit.jupiter.api.Test)40 EntityPersister (org.hibernate.persister.entity.EntityPersister)39 MappingMetamodelImplementor (org.hibernate.metamodel.spi.MappingMetamodelImplementor)36 JdbcMapping (org.hibernate.metamodel.mapping.JdbcMapping)29 JdbcTypeRegistry (org.hibernate.type.descriptor.jdbc.spi.JdbcTypeRegistry)17 BitSet (java.util.BitSet)7 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)4 JpaAttributeConverter (org.hibernate.metamodel.model.convert.spi.JpaAttributeConverter)4 ZoneOffset (java.time.ZoneOffset)2 Dialect (org.hibernate.dialect.Dialect)2 NationalizationSupport (org.hibernate.dialect.NationalizationSupport)2 EntityMappingType (org.hibernate.metamodel.mapping.EntityMappingType)2 ModelPart (org.hibernate.metamodel.mapping.ModelPart)2 BasicValueConverter (org.hibernate.metamodel.model.convert.spi.BasicValueConverter)2 Duration (java.time.Duration)1 Instant (java.time.Instant)1 LocalDate (java.time.LocalDate)1 LocalDateTime (java.time.LocalDateTime)1 LocalTime (java.time.LocalTime)1