Search in sources :

Example 16 with MappingMetamodelImplementor

use of org.hibernate.metamodel.spi.MappingMetamodelImplementor 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)

Example 17 with MappingMetamodelImplementor

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

the class CharacterArrayMappingTests 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(EntityWithCharArrays.class);
    {
        final BasicAttributeMapping attributeMapping = (BasicAttributeMapping) entityDescriptor.findAttributeMapping("primitive");
        final JdbcMapping jdbcMapping = attributeMapping.getJdbcMapping();
        assertThat(jdbcMapping.getJdbcType(), equalTo(jdbcRegistry.getDescriptor(Types.VARCHAR)));
    }
    {
        final BasicAttributeMapping attributeMapping = (BasicAttributeMapping) entityDescriptor.findAttributeMapping("wrapper");
        final JdbcMapping jdbcMapping = attributeMapping.getJdbcMapping();
        assertThat(jdbcMapping.getJdbcType(), equalTo(jdbcRegistry.getDescriptor(Types.VARCHAR)));
    }
    {
        final BasicAttributeMapping attributeMapping = (BasicAttributeMapping) entityDescriptor.findAttributeMapping("primitiveClob");
        final JdbcMapping jdbcMapping = attributeMapping.getJdbcMapping();
        assertThat(jdbcMapping.getJdbcType(), equalTo(jdbcRegistry.getDescriptor(Types.CLOB)));
    }
    {
        final BasicAttributeMapping attributeMapping = (BasicAttributeMapping) entityDescriptor.findAttributeMapping("wrapperClob");
        final JdbcMapping jdbcMapping = attributeMapping.getJdbcMapping();
        assertThat(jdbcMapping.getJdbcType(), equalTo(jdbcRegistry.getDescriptor(Types.CLOB)));
    }
}
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 18 with MappingMetamodelImplementor

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

the class CharacterMappingTests 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.getEntityDescriptor(EntityOfCharacters.class);
    {
        final BasicAttributeMapping attribute = (BasicAttributeMapping) entityDescriptor.findAttributeMapping("wrapper");
        assertThat(attribute.getJavaType().getJavaTypeClass(), equalTo(Character.class));
        final JdbcMapping jdbcMapping = attribute.getJdbcMapping();
        assertThat(jdbcMapping.getJavaTypeDescriptor().getJavaTypeClass(), equalTo(Character.class));
        assertThat(jdbcMapping.getJdbcType(), equalTo(jdbcRegistry.getDescriptor(Types.CHAR)));
    }
    {
        final BasicAttributeMapping attribute = (BasicAttributeMapping) entityDescriptor.findAttributeMapping("primitive");
        assertThat(attribute.getJavaType().getJavaTypeClass(), equalTo(Character.class));
        final JdbcMapping jdbcMapping = attribute.getJdbcMapping();
        assertThat(jdbcMapping.getJavaTypeDescriptor().getJavaTypeClass(), equalTo(Character.class));
        assertThat(jdbcMapping.getJdbcType(), equalTo(jdbcRegistry.getDescriptor(Types.CHAR)));
    }
    // and try to use the mapping
    scope.inTransaction((session) -> session.persist(new EntityOfCharacters(1, 'A', 'b')));
    scope.inTransaction((session) -> session.get(EntityOfCharacters.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 19 with MappingMetamodelImplementor

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

the class CoordinatingEntityNameResolver method resolveEntityName.

@Override
public String resolveEntityName(Object entity) {
    String entityName = interceptor.getEntityName(entity);
    if (entityName != null) {
        return entityName;
    }
    final MappingMetamodelImplementor mappingMetamodel = sessionFactory.getRuntimeMetamodels().getMappingMetamodel();
    for (EntityNameResolver resolver : mappingMetamodel.getEntityNameResolvers()) {
        entityName = resolver.resolveEntityName(entity);
        if (entityName != null) {
            break;
        }
    }
    if (entityName != null) {
        return entityName;
    }
    // the old-time stand-by...
    return entity.getClass().getName();
}
Also used : EntityNameResolver(org.hibernate.EntityNameResolver) MappingMetamodelImplementor(org.hibernate.metamodel.spi.MappingMetamodelImplementor)

Example 20 with MappingMetamodelImplementor

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

the class JoinedSubclassEntityPersister method pruneForSubclasses.

@Override
public void pruneForSubclasses(TableGroup tableGroup, Set<String> treatedEntityNames) {
    final Set<TableReference> retainedTableReferences = new HashSet<>(treatedEntityNames.size());
    final Set<String> sharedSuperclassTables = new HashSet<>();
    final MappingMetamodelImplementor metamodel = getFactory().getRuntimeMetamodels().getMappingMetamodel();
    for (String treatedEntityName : treatedEntityNames) {
        final JoinedSubclassEntityPersister subPersister = (JoinedSubclassEntityPersister) metamodel.findEntityDescriptor(treatedEntityName);
        final String[] subclassTableNames = subPersister.getSubclassTableNames();
        // In mathematical terms, sharedSuperclassTables will be the "intersection" of the table names of all treated entities
        if (sharedSuperclassTables.isEmpty()) {
            for (int i = 0; i < subclassTableNames.length; i++) {
                if (subPersister.isClassOrSuperclassTable[i]) {
                    sharedSuperclassTables.add(subclassTableNames[i]);
                }
            }
        } else {
            sharedSuperclassTables.retainAll(Arrays.asList(subclassTableNames));
        }
        // todo (6.0): no need to resolve all table references, only the ones needed for cardinality
        for (String subclassTableName : subclassTableNames) {
            retainedTableReferences.add(tableGroup.resolveTableReference(null, subclassTableName, false));
        }
    }
    final List<TableReferenceJoin> tableReferenceJoins = tableGroup.getTableReferenceJoins();
    // i.e. with parenthesis around, as that means the table reference joins will be isolated
    if (tableGroup.canUseInnerJoins() || tableGroup.isRealTableGroup()) {
        final TableReferenceJoin[] oldJoins = tableReferenceJoins.toArray(new TableReferenceJoin[0]);
        tableReferenceJoins.clear();
        for (TableReferenceJoin oldJoin : oldJoins) {
            final NamedTableReference joinedTableReference = oldJoin.getJoinedTableReference();
            if (retainedTableReferences.contains(joinedTableReference)) {
                if (oldJoin.getJoinType() != SqlAstJoinType.INNER && sharedSuperclassTables.contains(joinedTableReference.getTableExpression())) {
                    tableReferenceJoins.add(new TableReferenceJoin(true, joinedTableReference, oldJoin.getPredicate()));
                } else {
                    tableReferenceJoins.add(oldJoin);
                }
            }
        }
    } else {
        tableReferenceJoins.removeIf(join -> !retainedTableReferences.contains(join.getJoinedTableReference()));
    }
}
Also used : NamedTableReference(org.hibernate.sql.ast.tree.from.NamedTableReference) TableReferenceJoin(org.hibernate.sql.ast.tree.from.TableReferenceJoin) TableReference(org.hibernate.sql.ast.tree.from.TableReference) NamedTableReference(org.hibernate.sql.ast.tree.from.NamedTableReference) MappingMetamodelImplementor(org.hibernate.metamodel.spi.MappingMetamodelImplementor) HashSet(java.util.HashSet)

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