Search in sources :

Example 1 with MappingMetamodelImplementor

use of org.hibernate.metamodel.spi.MappingMetamodelImplementor in project hibernate-orm by hibernate.

the class AnyType method guessEntityPersister.

private EntityPersister guessEntityPersister(Object object) {
    if (typeConfiguration == null) {
        return null;
    }
    String entityName = null;
    // this code is largely copied from Session's bestGuessEntityName
    Object entity = object;
    if (entity instanceof HibernateProxy) {
        final LazyInitializer initializer = ((HibernateProxy) entity).getHibernateLazyInitializer();
        if (initializer.isUninitialized()) {
            entityName = initializer.getEntityName();
        }
        entity = initializer.getImplementation();
    }
    if (entityName == null) {
        final MappingMetamodelImplementor mappingMetamodel = typeConfiguration.getSessionFactory().getRuntimeMetamodels().getMappingMetamodel();
        for (EntityNameResolver resolver : mappingMetamodel.getEntityNameResolvers()) {
            entityName = resolver.resolveEntityName(entity);
            if (entityName != null) {
                break;
            }
        }
    }
    if (entityName == null) {
        // the old-time stand-by...
        entityName = object.getClass().getName();
    }
    return typeConfiguration.getSessionFactory().getRuntimeMetamodels().getMappingMetamodel().getEntityDescriptor(entityName);
}
Also used : LazyInitializer(org.hibernate.proxy.LazyInitializer) EntityNameResolver(org.hibernate.EntityNameResolver) MappingMetamodelImplementor(org.hibernate.metamodel.spi.MappingMetamodelImplementor) HibernateProxy(org.hibernate.proxy.HibernateProxy)

Example 2 with MappingMetamodelImplementor

use of org.hibernate.metamodel.spi.MappingMetamodelImplementor in project hibernate-orm by hibernate.

the class BitSetJdbcTypeRegistrationTests method verifyMappings.

@Test
public void verifyMappings(SessionFactoryScope scope) {
    final MappingMetamodelImplementor mappingMetamodel = scope.getSessionFactory().getRuntimeMetamodels().getMappingMetamodel();
    final EntityPersister entityDescriptor = mappingMetamodel.findEntityDescriptor(Product.class);
    final BasicAttributeMapping attributeMapping = (BasicAttributeMapping) entityDescriptor.findAttributeMapping("bitSet");
    assertThat(attributeMapping.getJavaType().getJavaTypeClass(), equalTo(BitSet.class));
    assertThat(attributeMapping.getValueConverter(), nullValue());
    assertThat(attributeMapping.getJdbcMapping().getJdbcType().getJdbcTypeCode(), is(Types.VARBINARY));
    assertThat(attributeMapping.getJdbcMapping().getJavaTypeDescriptor().getJavaTypeClass(), equalTo(BitSet.class));
    scope.inTransaction((session) -> {
        session.persist(new Product(1, BitSet.valueOf(BitSetHelper.BYTES)));
    });
    scope.inSession((session) -> {
        final Product product = session.get(Product.class, 1);
        assertThat(product.getBitSet(), equalTo(BitSet.valueOf(BitSetHelper.BYTES)));
    });
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) BasicAttributeMapping(org.hibernate.metamodel.mapping.internal.BasicAttributeMapping) BitSet(java.util.BitSet) MappingMetamodelImplementor(org.hibernate.metamodel.spi.MappingMetamodelImplementor) Test(org.junit.jupiter.api.Test)

Example 3 with MappingMetamodelImplementor

use of org.hibernate.metamodel.spi.MappingMetamodelImplementor in project hibernate-orm by hibernate.

the class BitSetConverterTests method verifyMappings.

@Test
public void verifyMappings(SessionFactoryScope scope) {
    final MappingMetamodelImplementor mappingMetamodel = scope.getSessionFactory().getRuntimeMetamodels().getMappingMetamodel();
    final EntityPersister entityDescriptor = mappingMetamodel.findEntityDescriptor(Product.class);
    final BasicAttributeMapping attributeMapping = (BasicAttributeMapping) entityDescriptor.findAttributeMapping("bitSet");
    assertThat(attributeMapping.getJavaType().getJavaTypeClass(), equalTo(BitSet.class));
    assertThat(attributeMapping.getValueConverter(), instanceOf(JpaAttributeConverter.class));
    final JpaAttributeConverter converter = (JpaAttributeConverter) attributeMapping.getValueConverter();
    assertThat(converter.getConverterBean().getBeanClass(), equalTo(BitSetConverter.class));
    Assertions.assertThat(attributeMapping.getExposedMutabilityPlan()).isNotInstanceOf(BitSetMutabilityPlan.class);
    assertThat(attributeMapping.getJdbcMapping().getJdbcType().getJdbcTypeCode(), isOneOf(Types.VARCHAR, Types.NVARCHAR));
    assertThat(attributeMapping.getJdbcMapping().getJavaTypeDescriptor().getJavaTypeClass(), equalTo(String.class));
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) JpaAttributeConverter(org.hibernate.metamodel.model.convert.spi.JpaAttributeConverter) BasicAttributeMapping(org.hibernate.metamodel.mapping.internal.BasicAttributeMapping) BitSet(java.util.BitSet) MappingMetamodelImplementor(org.hibernate.metamodel.spi.MappingMetamodelImplementor) Test(org.junit.jupiter.api.Test)

Example 4 with MappingMetamodelImplementor

use of org.hibernate.metamodel.spi.MappingMetamodelImplementor in project hibernate-orm by hibernate.

the class BitSetImplicitTests method verifyMappings.

@Test
public void verifyMappings(SessionFactoryScope scope) {
    final MappingMetamodelImplementor mappingMetamodel = scope.getSessionFactory().getRuntimeMetamodels().getMappingMetamodel();
    final EntityPersister entityDescriptor = mappingMetamodel.findEntityDescriptor(Product.class);
    final BasicAttributeMapping attributeMapping = (BasicAttributeMapping) entityDescriptor.findAttributeMapping("bitSet");
    assertThat(attributeMapping.getJavaType().getJavaTypeClass(), equalTo(BitSet.class));
    assertThat(attributeMapping.getValueConverter(), nullValue());
    assertThat(attributeMapping.getJdbcMapping().getJdbcType().getJdbcTypeCode(), is(Types.VARBINARY));
    // it will just be serialized
    assertThat(attributeMapping.getJdbcMapping().getJavaTypeDescriptor().getJavaTypeClass(), equalTo(BitSet.class));
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) BasicAttributeMapping(org.hibernate.metamodel.mapping.internal.BasicAttributeMapping) BitSet(java.util.BitSet) MappingMetamodelImplementor(org.hibernate.metamodel.spi.MappingMetamodelImplementor) Test(org.junit.jupiter.api.Test)

Example 5 with MappingMetamodelImplementor

use of org.hibernate.metamodel.spi.MappingMetamodelImplementor in project hibernate-orm by hibernate.

the class StringNationalizedMappingTests 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(EntityOfStrings.class);
    final JdbcTypeRegistry jdbcTypeRegistry = mappingMetamodel.getTypeConfiguration().getJdbcTypeRegistry();
    final Dialect dialect = scope.getSessionFactory().getJdbcServices().getDialect();
    final NationalizationSupport nationalizationSupport = dialect.getNationalizationSupport();
    {
        final BasicAttributeMapping attribute = (BasicAttributeMapping) entityDescriptor.findAttributeMapping("nstring");
        final JdbcMapping jdbcMapping = attribute.getJdbcMapping();
        assertThat(jdbcMapping.getJavaTypeDescriptor().getJavaTypeClass(), equalTo(String.class));
        assertThat(jdbcMapping.getJdbcType(), is(jdbcTypeRegistry.getDescriptor(nationalizationSupport.getVarcharVariantCode())));
    }
    {
        final BasicAttributeMapping attribute = (BasicAttributeMapping) entityDescriptor.findAttributeMapping("nclobString");
        final JdbcMapping jdbcMapping = attribute.getJdbcMapping();
        assertThat(jdbcMapping.getJavaTypeDescriptor().getJavaTypeClass(), equalTo(String.class));
        assertThat(jdbcMapping.getJdbcType(), is(jdbcTypeRegistry.getDescriptor(nationalizationSupport.getClobVariantCode())));
    }
    // and try to use the mapping
    scope.inTransaction((session) -> session.persist(new EntityOfStrings(1, "nstring", "nclob")));
    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) Dialect(org.hibernate.dialect.Dialect) MappingMetamodelImplementor(org.hibernate.metamodel.spi.MappingMetamodelImplementor) NationalizationSupport(org.hibernate.dialect.NationalizationSupport) JdbcTypeRegistry(org.hibernate.type.descriptor.jdbc.spi.JdbcTypeRegistry) Test(org.junit.jupiter.api.Test)

Aggregations

MappingMetamodelImplementor (org.hibernate.metamodel.spi.MappingMetamodelImplementor)49 EntityPersister (org.hibernate.persister.entity.EntityPersister)40 Test (org.junit.jupiter.api.Test)37 BasicAttributeMapping (org.hibernate.metamodel.mapping.internal.BasicAttributeMapping)36 JdbcMapping (org.hibernate.metamodel.mapping.JdbcMapping)29 JdbcTypeRegistry (org.hibernate.type.descriptor.jdbc.spi.JdbcTypeRegistry)14 BitSet (java.util.BitSet)7 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)6 CollectionPersister (org.hibernate.persister.collection.CollectionPersister)4 Dialect (org.hibernate.dialect.Dialect)3 Type (org.hibernate.type.Type)3 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 EntityNameResolver (org.hibernate.EntityNameResolver)2 HibernateException (org.hibernate.HibernateException)2 CollectionDataAccess (org.hibernate.cache.spi.access.CollectionDataAccess)2 SoftLock (org.hibernate.cache.spi.access.SoftLock)2 EntityEntry (org.hibernate.engine.spi.EntityEntry)2 JpaAttributeConverter (org.hibernate.metamodel.model.convert.spi.JpaAttributeConverter)2 NamedTableReference (org.hibernate.sql.ast.tree.from.NamedTableReference)2