Search in sources :

Example 6 with EntityDiscriminatorMapping

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

the class SqmCteTable method createEntityTable.

public static SqmCteTable createEntityTable(String cteName, EntityMappingType entityDescriptor) {
    return new SqmCteTable(cteName, sqmCteTable -> {
        final int numberOfColumns = entityDescriptor.getIdentifierMapping().getJdbcTypeCount();
        final List<SqmCteTableColumn> columns = new ArrayList<>(numberOfColumns);
        final EntityIdentifierMapping identifierMapping = entityDescriptor.getIdentifierMapping();
        final String idName;
        if (identifierMapping instanceof SingleAttributeIdentifierMapping) {
            idName = ((SingleAttributeIdentifierMapping) identifierMapping).getAttributeName();
        } else {
            idName = "id";
        }
        columns.add(new SqmCteTableColumn(sqmCteTable, idName, identifierMapping));
        final EntityDiscriminatorMapping discriminatorMapping = entityDescriptor.getDiscriminatorMapping();
        if (discriminatorMapping != null && discriminatorMapping.isPhysical() && !discriminatorMapping.isFormula()) {
            columns.add(new SqmCteTableColumn(sqmCteTable, "class", discriminatorMapping));
        }
        // Collect all columns for all entity subtype attributes
        entityDescriptor.visitSubTypeAttributeMappings(attribute -> {
            if (!(attribute instanceof PluralAttributeMapping)) {
                columns.add(new SqmCteTableColumn(sqmCteTable, attribute.getAttributeName(), attribute));
            }
        });
        // We add a special row number column that we can use to identify and join rows
        columns.add(new SqmCteTableColumn(sqmCteTable, "rn_", entityDescriptor.getEntityPersister().getFactory().getTypeConfiguration().getBasicTypeForJavaType(Integer.class)));
        return columns;
    });
}
Also used : ArrayList(java.util.ArrayList) EntityIdentifierMapping(org.hibernate.metamodel.mapping.EntityIdentifierMapping) PluralAttributeMapping(org.hibernate.metamodel.mapping.PluralAttributeMapping) SingleAttributeIdentifierMapping(org.hibernate.metamodel.mapping.internal.SingleAttributeIdentifierMapping) EntityDiscriminatorMapping(org.hibernate.metamodel.mapping.EntityDiscriminatorMapping)

Example 7 with EntityDiscriminatorMapping

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

the class AbstractEntityResultGraphNode method afterInitialize.

@Override
public void afterInitialize(FetchParent fetchParent, DomainResultCreationState creationState) {
    final EntityMappingType entityDescriptor = referencedModelPart.getEntityMappingType();
    final EntityIdentifierMapping identifierMapping = entityDescriptor.getIdentifierMapping();
    final NavigablePath navigablePath = getNavigablePath();
    final TableGroup entityTableGroup = creationState.getSqlAstCreationState().getFromClauseAccess().getTableGroup(navigablePath);
    final EntityIdentifierNavigablePath identifierNavigablePath = new EntityIdentifierNavigablePath(navigablePath, attributeName(identifierMapping));
    if (navigablePath.getParent() == null && !creationState.forceIdentifierSelection()) {
        identifierFetch = null;
        visitIdentifierMapping(identifierNavigablePath, creationState, identifierMapping, entityTableGroup);
    } else {
        identifierFetch = ((Fetchable) identifierMapping).generateFetch(fetchParent, identifierNavigablePath, FetchTiming.IMMEDIATE, true, null, creationState);
    }
    final EntityDiscriminatorMapping discriminatorMapping = entityDescriptor.getDiscriminatorMapping();
    // No need to fetch the discriminator if this type does not have subclasses
    if (discriminatorMapping != null && entityDescriptor.hasSubclasses()) {
        discriminatorFetch = discriminatorMapping.generateFetch(fetchParent, navigablePath.append(EntityDiscriminatorMapping.ROLE_NAME), FetchTiming.IMMEDIATE, true, null, creationState);
    } else {
        discriminatorFetch = null;
    }
    final EntityRowIdMapping rowIdMapping = entityDescriptor.getRowIdMapping();
    if (rowIdMapping == null) {
        rowIdResult = null;
    } else {
        rowIdResult = rowIdMapping.createDomainResult(navigablePath.append(rowIdMapping.getRowIdName()), entityTableGroup, AbstractEntityPersister.ROWID_ALIAS, creationState);
    }
    super.afterInitialize(fetchParent, creationState);
}
Also used : NavigablePath(org.hibernate.query.spi.NavigablePath) EntityIdentifierNavigablePath(org.hibernate.query.sqm.spi.EntityIdentifierNavigablePath) TableGroup(org.hibernate.sql.ast.tree.from.TableGroup) EntityIdentifierNavigablePath(org.hibernate.query.sqm.spi.EntityIdentifierNavigablePath) EntityIdentifierMapping(org.hibernate.metamodel.mapping.EntityIdentifierMapping) EntityRowIdMapping(org.hibernate.metamodel.mapping.EntityRowIdMapping) EntityMappingType(org.hibernate.metamodel.mapping.EntityMappingType) EntityDiscriminatorMapping(org.hibernate.metamodel.mapping.EntityDiscriminatorMapping)

Aggregations

EntityDiscriminatorMapping (org.hibernate.metamodel.mapping.EntityDiscriminatorMapping)7 ArrayList (java.util.ArrayList)4 EntityIdentifierMapping (org.hibernate.metamodel.mapping.EntityIdentifierMapping)4 IdentifierGenerator (org.hibernate.id.IdentifierGenerator)3 PostInsertIdentifierGenerator (org.hibernate.id.PostInsertIdentifierGenerator)3 BasicValuedModelPart (org.hibernate.metamodel.mapping.BasicValuedModelPart)3 EntityMappingType (org.hibernate.metamodel.mapping.EntityMappingType)3 PluralAttributeMapping (org.hibernate.metamodel.mapping.PluralAttributeMapping)3 NavigablePath (org.hibernate.query.spi.NavigablePath)3 Collections (java.util.Collections)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Consumer (java.util.function.Consumer)2 OptimizableGenerator (org.hibernate.id.OptimizableGenerator)2 Optimizer (org.hibernate.id.enhanced.Optimizer)2 JdbcMapping (org.hibernate.metamodel.mapping.JdbcMapping)2 ColumnReference (org.hibernate.sql.ast.tree.expression.ColumnReference)2 Expression (org.hibernate.sql.ast.tree.expression.Expression)2 TableGroup (org.hibernate.sql.ast.tree.from.TableGroup)2 Assignable (org.hibernate.sql.ast.tree.update.Assignable)2