Search in sources :

Example 11 with BasicAttributeMapping

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

the class LongMappingTests 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(EntityOfLongs.class);
    {
        final BasicAttributeMapping attribute = (BasicAttributeMapping) entityDescriptor.findAttributeMapping("wrapper");
        assertThat(attribute.getJavaType().getJavaTypeClass(), equalTo(Long.class));
        final JdbcMapping jdbcMapping = attribute.getJdbcMapping();
        assertThat(jdbcMapping.getJavaTypeDescriptor().getJavaTypeClass(), equalTo(Long.class));
        assertThat(jdbcMapping.getJdbcType().getJdbcTypeCode(), is(Types.BIGINT));
    }
    {
        final BasicAttributeMapping attribute = (BasicAttributeMapping) entityDescriptor.findAttributeMapping("primitive");
        assertThat(attribute.getJavaType().getJavaTypeClass(), equalTo(Long.class));
        final JdbcMapping jdbcMapping = attribute.getJdbcMapping();
        assertThat(jdbcMapping.getJavaTypeDescriptor().getJavaTypeClass(), equalTo(Long.class));
        assertThat(jdbcMapping.getJdbcType().getJdbcTypeCode(), is(Types.BIGINT));
    }
    // and try to use the mapping
    scope.inTransaction((session) -> session.persist(new EntityOfLongs(1, 3L, 5L)));
    scope.inTransaction((session) -> session.get(EntityOfLongs.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 12 with BasicAttributeMapping

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

the class OffsetDateTimeMappingTests method verifyMappings.

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

Example 13 with BasicAttributeMapping

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

the class ClassMappingTests 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.findEntityDescriptor(EntityWithClass.class);
    final BasicAttributeMapping duration = (BasicAttributeMapping) entityDescriptor.findAttributeMapping("clazz");
    final JdbcMapping jdbcMapping = duration.getJdbcMapping();
    assertThat(jdbcMapping.getJavaTypeDescriptor().getJavaTypeClass(), equalTo(Class.class));
    assertThat(jdbcMapping.getJdbcType(), equalTo(jdbcRegistry.getDescriptor(Types.VARCHAR)));
    scope.inTransaction((session) -> {
        session.persist(new EntityWithClass(1, String.class));
    });
    scope.inTransaction((session) -> session.find(EntityWithClass.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 14 with BasicAttributeMapping

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

the class DurationMappingTests method verifyMappings.

@Test
public void verifyMappings(SessionFactoryScope scope) {
    final MappingMetamodelImplementor mappingMetamodel = scope.getSessionFactory().getRuntimeMetamodels().getMappingMetamodel();
    final EntityPersister entityDescriptor = mappingMetamodel.findEntityDescriptor(EntityWithDuration.class);
    final JdbcTypeRegistry jdbcTypeRegistry = mappingMetamodel.getTypeConfiguration().getJdbcTypeRegistry();
    final BasicAttributeMapping duration = (BasicAttributeMapping) entityDescriptor.findAttributeMapping("duration");
    final JdbcMapping jdbcMapping = duration.getJdbcMapping();
    assertThat(jdbcMapping.getJavaTypeDescriptor().getJavaTypeClass(), equalTo(Duration.class));
    final JdbcType intervalType = jdbcTypeRegistry.getDescriptor(SqlTypes.INTERVAL_SECOND);
    final JdbcType realType;
    if (intervalType instanceof AdjustableJdbcType) {
        realType = ((AdjustableJdbcType) intervalType).resolveIndicatedType(() -> mappingMetamodel.getTypeConfiguration(), jdbcMapping.getJavaTypeDescriptor());
    } else {
        realType = intervalType;
    }
    assertThat(jdbcMapping.getJdbcType(), is(realType));
    scope.inTransaction((session) -> {
        session.persist(new EntityWithDuration(1, Duration.ofHours(3)));
    });
    scope.inTransaction((session) -> session.find(EntityWithDuration.class, 1));
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) JdbcMapping(org.hibernate.metamodel.mapping.JdbcMapping) BasicAttributeMapping(org.hibernate.metamodel.mapping.internal.BasicAttributeMapping) AdjustableJdbcType(org.hibernate.type.descriptor.jdbc.AdjustableJdbcType) JdbcType(org.hibernate.type.descriptor.jdbc.JdbcType) AdjustableJdbcType(org.hibernate.type.descriptor.jdbc.AdjustableJdbcType) MappingMetamodelImplementor(org.hibernate.metamodel.spi.MappingMetamodelImplementor) Duration(java.time.Duration) JdbcTypeRegistry(org.hibernate.type.descriptor.jdbc.spi.JdbcTypeRegistry) Test(org.junit.jupiter.api.Test)

Example 15 with BasicAttributeMapping

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

the class ByteMappingTests 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(EntityOfBytes.class);
    final JdbcTypeRegistry jdbcTypeRegistry = mappingMetamodel.getTypeConfiguration().getJdbcTypeRegistry();
    {
        final BasicAttributeMapping attribute = (BasicAttributeMapping) entityDescriptor.findAttributeMapping("wrapper");
        assertThat(attribute.getJavaType().getJavaTypeClass(), equalTo(Byte.class));
        final JdbcMapping jdbcMapping = attribute.getJdbcMapping();
        assertThat(jdbcMapping.getJavaTypeDescriptor().getJavaTypeClass(), equalTo(Byte.class));
        assertThat(jdbcMapping.getJdbcType(), is(jdbcTypeRegistry.getDescriptor(Types.TINYINT)));
    }
    {
        final BasicAttributeMapping attribute = (BasicAttributeMapping) entityDescriptor.findAttributeMapping("primitive");
        assertThat(attribute.getJavaType().getJavaTypeClass(), equalTo(Byte.class));
        final JdbcMapping jdbcMapping = attribute.getJdbcMapping();
        assertThat(jdbcMapping.getJavaTypeDescriptor().getJavaTypeClass(), equalTo(Byte.class));
        assertThat(jdbcMapping.getJdbcType(), is(jdbcTypeRegistry.getDescriptor(Types.TINYINT)));
    }
    // and try to use the mapping
    scope.inTransaction((session) -> session.persist(new EntityOfBytes(1, (byte) 3, (byte) 5)));
    scope.inTransaction((session) -> session.get(EntityOfBytes.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)

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