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));
}
}
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));
}
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);
});
}
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));
}
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));
}
Aggregations