Search in sources :

Example 31 with BasicAttributeMapping

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

the class BooleanMappingTests method verifyMappings.

@Test
public void verifyMappings(SessionFactoryScope scope) {
    final MappingMetamodelImplementor mappingMetamodel = scope.getSessionFactory().getRuntimeMetamodels().getMappingMetamodel();
    final EntityPersister entityDescriptor = mappingMetamodel.getEntityDescriptor(EntityOfBooleans.class);
    {
        final BasicAttributeMapping implicit = (BasicAttributeMapping) entityDescriptor.findAttributeMapping("implicit");
        final JdbcMapping jdbcMapping = implicit.getJdbcMapping();
        assertThat(jdbcMapping.getJavaTypeDescriptor().getJavaType(), equalTo(Boolean.class));
        assertThat(jdbcMapping.getJdbcType().getJdbcTypeCode(), // the implicit mapping will depend on the Dialect
        isOneOf(Types.BOOLEAN, Types.BIT, Types.TINYINT));
    }
    // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    // Converters
    {
        final BasicAttributeMapping convertedYesNo = (BasicAttributeMapping) entityDescriptor.findAttributeMapping("convertedYesNo");
        final JdbcMapping jdbcMapping = convertedYesNo.getJdbcMapping();
        assertThat(jdbcMapping.getJavaTypeDescriptor().getJavaType(), equalTo(Character.class));
        assertThat(jdbcMapping.getJdbcType().getJdbcTypeCode(), // could be NCHAR if nationalization is globally enabled
        isOneOf(Types.CHAR, Types.NCHAR));
    }
    {
        final BasicAttributeMapping convertedTrueFalse = (BasicAttributeMapping) entityDescriptor.findAttributeMapping("convertedTrueFalse");
        final JdbcMapping jdbcMapping = convertedTrueFalse.getJdbcMapping();
        assertThat(jdbcMapping.getJavaTypeDescriptor().getJavaType(), equalTo(Character.class));
        assertThat(jdbcMapping.getJdbcType().getJdbcTypeCode(), // could be NCHAR if nationalization is globally enabled
        isOneOf(Types.CHAR, Types.NCHAR));
    }
    {
        final BasicAttributeMapping convertedNumeric = (BasicAttributeMapping) entityDescriptor.findAttributeMapping("convertedNumeric");
        final JdbcMapping jdbcMapping = convertedNumeric.getJdbcMapping();
        assertThat(jdbcMapping.getJavaTypeDescriptor().getJavaType(), equalTo(Integer.class));
        assertThat(jdbcMapping.getJdbcType().getJdbcTypeCode(), equalTo(Types.INTEGER));
    }
}
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 32 with BasicAttributeMapping

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

the class CurrencyMappingTests method verifyMappings.

@Test
public void verifyMappings(SessionFactoryScope scope) {
    final MappingMetamodelImplementor mappingMetamodel = scope.getSessionFactory().getRuntimeMetamodels().getMappingMetamodel();
    final JdbcTypeRegistry jdbcRegistry = mappingMetamodel.getTypeConfiguration().getJdbcTypeRegistry();
    final EntityPersister entityDescriptor = mappingMetamodel.getEntityDescriptor(EntityWithCurrency.class);
    final BasicAttributeMapping duration = (BasicAttributeMapping) entityDescriptor.findAttributeMapping("currency");
    final JdbcMapping jdbcMapping = duration.getJdbcMapping();
    assertThat(jdbcMapping.getJavaTypeDescriptor().getJavaTypeClass(), equalTo(Currency.class));
    assertThat(jdbcMapping.getJdbcType(), equalTo(jdbcRegistry.getDescriptor(Types.VARCHAR)));
    scope.inTransaction((session) -> {
        session.persist(new EntityWithCurrency(1, Currency.getInstance("USD")));
    });
    scope.inTransaction((session) -> session.find(EntityWithCurrency.class, 1));
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) JdbcMapping(org.hibernate.metamodel.mapping.JdbcMapping) BasicAttributeMapping(org.hibernate.metamodel.mapping.internal.BasicAttributeMapping) Currency(java.util.Currency) MappingMetamodelImplementor(org.hibernate.metamodel.spi.MappingMetamodelImplementor) JdbcTypeRegistry(org.hibernate.type.descriptor.jdbc.spi.JdbcTypeRegistry) Test(org.junit.jupiter.api.Test)

Example 33 with BasicAttributeMapping

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

the class DatePrecisionTests method verifyMapping.

@Test
public void verifyMapping(SessionFactoryScope scope) {
    final MappingMetamodelImplementor mappingMetamodel = scope.getSessionFactory().getRuntimeMetamodels().getMappingMetamodel();
    final EntityPersister entityDescriptor = mappingMetamodel.findEntityDescriptor(EntityOfDates.class);
    {
        final BasicAttributeMapping attribute = (BasicAttributeMapping) entityDescriptor.findAttributeMapping("dateAsTimestamp");
        final JdbcMapping jdbcMapping = attribute.getJdbcMapping();
        final TemporalJavaType jtd = (TemporalJavaType) jdbcMapping.getJavaTypeDescriptor();
        assertThat(jtd, is(attribute.getJavaType()));
        assertThat(jtd.getJavaTypeClass(), equalTo(Timestamp.class));
        assertThat(jtd.getPrecision(), equalTo(TemporalType.TIMESTAMP));
        assertThat(jdbcMapping.getJdbcType().getJdbcTypeCode(), equalTo(Types.TIMESTAMP));
    }
    {
        final BasicAttributeMapping attribute = (BasicAttributeMapping) entityDescriptor.findAttributeMapping("dateAsDate");
        final JdbcMapping jdbcMapping = attribute.getJdbcMapping();
        final TemporalJavaType jtd = (TemporalJavaType) jdbcMapping.getJavaTypeDescriptor();
        assertThat(jtd, is(attribute.getJavaType()));
        assertThat(jtd.getJavaTypeClass(), equalTo(java.sql.Date.class));
        assertThat(jtd.getPrecision(), equalTo(TemporalType.DATE));
        assertThat(jdbcMapping.getJdbcType().getJdbcTypeCode(), equalTo(Types.DATE));
    }
    {
        final BasicAttributeMapping attribute = (BasicAttributeMapping) entityDescriptor.findAttributeMapping("dateAsTime");
        final JdbcMapping jdbcMapping = attribute.getJdbcMapping();
        final TemporalJavaType jtd = (TemporalJavaType) jdbcMapping.getJavaTypeDescriptor();
        assertThat(jtd, is(attribute.getJavaType()));
        assertThat(jtd.getJavaTypeClass(), equalTo(Time.class));
        assertThat(jtd.getPrecision(), equalTo(TemporalType.TIME));
        assertThat(jdbcMapping.getJdbcType().getJdbcTypeCode(), equalTo(Types.TIME));
    }
    // check persistence operations
    scope.inTransaction((session) -> {
        session.persist(new EntityOfDates(1, Date.from(Instant.now()), Date.from(Instant.now()), Date.from(Instant.now())));
    });
    scope.inTransaction((session) -> {
        session.find(EntityOfDates.class, 1);
    });
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) TemporalJavaType(org.hibernate.type.descriptor.java.TemporalJavaType) 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 34 with BasicAttributeMapping

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

the class DoubleMappingTests method testMappings.

@Test
public void testMappings(SessionFactoryScope scope) {
    // first, verify the type selections...
    final MappingMetamodelImplementor mappingMetamodel = scope.getSessionFactory().getRuntimeMetamodels().getMappingMetamodel();
    final EntityPersister entityDescriptor = mappingMetamodel.getEntityDescriptor(EntityOfDoubles.class);
    {
        final BasicAttributeMapping attribute = (BasicAttributeMapping) entityDescriptor.findAttributeMapping("wrapper");
        assertThat(attribute.getJavaType().getJavaTypeClass(), equalTo(Double.class));
        final JdbcMapping jdbcMapping = attribute.getJdbcMapping();
        assertThat(jdbcMapping.getJavaTypeDescriptor().getJavaTypeClass(), equalTo(Double.class));
        assertThat(jdbcMapping.getJdbcType().getJdbcTypeCode(), isOneOf(Types.DOUBLE, Types.FLOAT, Types.REAL, Types.NUMERIC));
    }
    {
        final BasicAttributeMapping attribute = (BasicAttributeMapping) entityDescriptor.findAttributeMapping("primitive");
        assertThat(attribute.getJavaType().getJavaTypeClass(), equalTo(Double.class));
        final JdbcMapping jdbcMapping = attribute.getJdbcMapping();
        assertThat(jdbcMapping.getJavaTypeDescriptor().getJavaTypeClass(), equalTo(Double.class));
        assertThat(jdbcMapping.getJdbcType().getJdbcTypeCode(), isOneOf(Types.DOUBLE, Types.FLOAT, Types.REAL, Types.NUMERIC));
    }
    // and try to use the mapping
    scope.inTransaction((session) -> session.persist(new EntityOfDoubles(1, 3.0, 5.0)));
    scope.inTransaction((session) -> session.get(EntityOfDoubles.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 35 with BasicAttributeMapping

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

the class OffsetTimeMappingTests method verifyMappings.

@Test
public void verifyMappings(SessionFactoryScope scope) {
    final MappingMetamodelImplementor mappingMetamodel = scope.getSessionFactory().getRuntimeMetamodels().getMappingMetamodel();
    final EntityPersister entityDescriptor = mappingMetamodel.findEntityDescriptor(EntityWithOffsetTime.class);
    final BasicAttributeMapping attributeMapping = (BasicAttributeMapping) entityDescriptor.findAttributeMapping("offsetTime");
    final JdbcMapping jdbcMapping = attributeMapping.getJdbcMapping();
    assertThat(jdbcMapping.getJavaTypeDescriptor().getJavaTypeClass(), equalTo(OffsetTime.class));
    assertThat(jdbcMapping.getJdbcType().getJdbcTypeCode(), isOneOf(Types.TIME, Types.TIME_WITH_TIMEZONE));
    scope.inTransaction((session) -> {
        session.persist(new EntityWithOffsetTime(1, OffsetTime.now()));
    });
    scope.inTransaction((session) -> session.find(EntityWithOffsetTime.class, 1));
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) JdbcMapping(org.hibernate.metamodel.mapping.JdbcMapping) OffsetTime(java.time.OffsetTime) BasicAttributeMapping(org.hibernate.metamodel.mapping.internal.BasicAttributeMapping) MappingMetamodelImplementor(org.hibernate.metamodel.spi.MappingMetamodelImplementor) 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