Search in sources :

Example 1 with DenormalizedTable

use of org.hibernate.mapping.DenormalizedTable in project hibernate-orm by hibernate.

the class ModelBinder method bindEntityTableSpecification.

private Table bindEntityTableSpecification(final MappingDocument mappingDocument, TableSpecificationSource tableSpecSource, Table denormalizedSuperTable, final EntitySource entitySource, PersistentClass entityDescriptor) {
    final Namespace namespace = database.locateNamespace(determineCatalogName(tableSpecSource), determineSchemaName(tableSpecSource));
    final boolean isTable = TableSource.class.isInstance(tableSpecSource);
    final boolean isAbstract = entityDescriptor.isAbstract() == null ? false : entityDescriptor.isAbstract();
    final String subselect;
    final Identifier logicalTableName;
    final Table table;
    if (isTable) {
        final TableSource tableSource = (TableSource) tableSpecSource;
        if (StringHelper.isNotEmpty(tableSource.getExplicitTableName())) {
            logicalTableName = database.toIdentifier(tableSource.getExplicitTableName());
        } else {
            final ImplicitEntityNameSource implicitNamingSource = new ImplicitEntityNameSource() {

                @Override
                public EntityNaming getEntityNaming() {
                    return entitySource.getEntityNamingSource();
                }

                @Override
                public MetadataBuildingContext getBuildingContext() {
                    return mappingDocument;
                }
            };
            logicalTableName = mappingDocument.getBuildingOptions().getImplicitNamingStrategy().determinePrimaryTableName(implicitNamingSource);
        }
        if (denormalizedSuperTable == null) {
            table = namespace.createTable(logicalTableName, isAbstract);
        } else {
            table = namespace.createDenormalizedTable(logicalTableName, isAbstract, denormalizedSuperTable);
        }
    } else {
        final InLineViewSource inLineViewSource = (InLineViewSource) tableSpecSource;
        subselect = inLineViewSource.getSelectStatement();
        logicalTableName = database.toIdentifier(inLineViewSource.getLogicalName());
        if (denormalizedSuperTable == null) {
            table = new Table(namespace, subselect, isAbstract);
        } else {
            table = new DenormalizedTable(namespace, subselect, isAbstract, denormalizedSuperTable);
        }
        table.setName(logicalTableName.render());
    }
    EntityTableXref superEntityTableXref = null;
    if (entitySource.getSuperType() != null) {
        // noinspection SuspiciousMethodCalls
        final String superEntityName = ((EntitySource) entitySource.getSuperType()).getEntityNamingSource().getEntityName();
        superEntityTableXref = mappingDocument.getMetadataCollector().getEntityTableXref(superEntityName);
        if (superEntityTableXref == null) {
            throw new MappingException(String.format(Locale.ENGLISH, "Unable to locate entity table xref for entity [%s] super-type [%s]", entityDescriptor.getEntityName(), superEntityName), mappingDocument.getOrigin());
        }
    }
    mappingDocument.getMetadataCollector().addEntityTableXref(entitySource.getEntityNamingSource().getEntityName(), logicalTableName, table, superEntityTableXref);
    if (isTable) {
        final TableSource tableSource = (TableSource) tableSpecSource;
        table.setRowId(tableSource.getRowId());
        if (StringHelper.isNotEmpty(tableSource.getCheckConstraint())) {
            table.addCheckConstraint(tableSource.getCheckConstraint());
        }
    }
    table.setComment(tableSpecSource.getComment());
    mappingDocument.getMetadataCollector().addTableNameBinding(logicalTableName, table);
    return table;
}
Also used : Identifier(org.hibernate.boot.model.naming.Identifier) Table(org.hibernate.mapping.Table) DenormalizedTable(org.hibernate.mapping.DenormalizedTable) TableSource(org.hibernate.boot.model.source.spi.TableSource) SecondaryTableSource(org.hibernate.boot.model.source.spi.SecondaryTableSource) DenormalizedTable(org.hibernate.mapping.DenormalizedTable) EntityTableXref(org.hibernate.boot.spi.InFlightMetadataCollector.EntityTableXref) ImplicitEntityNameSource(org.hibernate.boot.model.naming.ImplicitEntityNameSource) InLineViewSource(org.hibernate.boot.model.source.spi.InLineViewSource) Namespace(org.hibernate.boot.model.relational.Namespace) MappingException(org.hibernate.boot.MappingException)

Example 2 with DenormalizedTable

use of org.hibernate.mapping.DenormalizedTable in project hibernate-orm by hibernate.

the class InFlightMetadataCollectorImpl method getLogicalColumnName.

@Override
public String getLogicalColumnName(Table table, Identifier physicalName) throws MappingException {
    final String physicalNameString = physicalName.render(getDatabase().getJdbcEnvironment().getDialect());
    Identifier logicalName = null;
    Table currentTable = table;
    while (currentTable != null) {
        final TableColumnNameBinding binding = columnNameBindingByTableMap.get(currentTable);
        if (binding != null) {
            logicalName = binding.physicalToLogical.get(physicalNameString);
            if (logicalName != null) {
                break;
            }
        }
        if (DenormalizedTable.class.isInstance(currentTable)) {
            currentTable = ((DenormalizedTable) currentTable).getIncludedTable();
        } else {
            currentTable = null;
        }
    }
    if (logicalName == null) {
        throw new MappingException("Unable to find column with physical name " + physicalNameString + " in table " + table.getName());
    }
    return logicalName.render();
}
Also used : Identifier(org.hibernate.boot.model.naming.Identifier) Table(org.hibernate.mapping.Table) DenormalizedTable(org.hibernate.mapping.DenormalizedTable) DuplicateMappingException(org.hibernate.DuplicateMappingException) MappingException(org.hibernate.MappingException)

Example 3 with DenormalizedTable

use of org.hibernate.mapping.DenormalizedTable in project hibernate-orm by hibernate.

the class InFlightMetadataCollectorImpl method addDenormalizedTable.

@Override
public Table addDenormalizedTable(String schemaName, String catalogName, String name, boolean isAbstract, String subselectFragment, Table includedTable) throws DuplicateMappingException {
    final Namespace namespace = getDatabase().locateNamespace(getDatabase().toIdentifier(catalogName), getDatabase().toIdentifier(schemaName));
    // annotation binding depends on the "table name" for @Subselect bindings
    // being set into the generated table (mainly to avoid later NPE), but for now we need to keep that :(
    final Identifier logicalName;
    if (name != null) {
        logicalName = getDatabase().toIdentifier(name);
    } else {
        logicalName = null;
    }
    if (subselectFragment != null) {
        return new DenormalizedTable(namespace, logicalName, subselectFragment, isAbstract, includedTable);
    } else {
        Table table = namespace.locateTable(logicalName);
        if (table != null) {
            throw new DuplicateMappingException(DuplicateMappingException.Type.TABLE, logicalName.toString());
        } else {
            table = namespace.createDenormalizedTable(logicalName, isAbstract, includedTable);
        }
        return table;
    }
}
Also used : Identifier(org.hibernate.boot.model.naming.Identifier) DenormalizedTable(org.hibernate.mapping.DenormalizedTable) Table(org.hibernate.mapping.Table) DenormalizedTable(org.hibernate.mapping.DenormalizedTable) DuplicateMappingException(org.hibernate.DuplicateMappingException) Namespace(org.hibernate.boot.model.relational.Namespace)

Example 4 with DenormalizedTable

use of org.hibernate.mapping.DenormalizedTable in project hibernate-orm by hibernate.

the class Namespace method createDenormalizedTable.

public DenormalizedTable createDenormalizedTable(Identifier logicalTableName, boolean isAbstract, Table includedTable) {
    final Table existing = tables.get(logicalTableName);
    if (existing != null) {
        // for now assume it is
        return (DenormalizedTable) existing;
    }
    final Identifier physicalTableName = database.getPhysicalNamingStrategy().toPhysicalTableName(logicalTableName, database.getJdbcEnvironment());
    DenormalizedTable table = new DenormalizedTable(this, physicalTableName, isAbstract, includedTable);
    tables.put(logicalTableName, table);
    return table;
}
Also used : DenormalizedTable(org.hibernate.mapping.DenormalizedTable) Table(org.hibernate.mapping.Table) DenormalizedTable(org.hibernate.mapping.DenormalizedTable) Identifier(org.hibernate.boot.model.naming.Identifier)

Example 5 with DenormalizedTable

use of org.hibernate.mapping.DenormalizedTable in project hibernate-orm by hibernate.

the class InFlightMetadataCollectorImpl method getPhysicalColumnName.

@Override
public String getPhysicalColumnName(Table table, Identifier logicalName) throws MappingException {
    if (logicalName == null) {
        throw new MappingException("Logical column name cannot be null");
    }
    Table currentTable = table;
    String physicalName = null;
    while (currentTable != null) {
        final TableColumnNameBinding binding = columnNameBindingByTableMap.get(currentTable);
        if (binding != null) {
            physicalName = binding.logicalToPhysical.get(logicalName);
            if (physicalName != null) {
                break;
            }
        }
        if (DenormalizedTable.class.isInstance(currentTable)) {
            currentTable = ((DenormalizedTable) currentTable).getIncludedTable();
        } else {
            currentTable = null;
        }
    }
    if (physicalName == null) {
        throw new MappingException("Unable to find column with logical name " + logicalName.render() + " in table " + table.getName());
    }
    return physicalName;
}
Also used : Table(org.hibernate.mapping.Table) DenormalizedTable(org.hibernate.mapping.DenormalizedTable) DuplicateMappingException(org.hibernate.DuplicateMappingException) MappingException(org.hibernate.MappingException)

Aggregations

DenormalizedTable (org.hibernate.mapping.DenormalizedTable)5 Table (org.hibernate.mapping.Table)5 Identifier (org.hibernate.boot.model.naming.Identifier)4 DuplicateMappingException (org.hibernate.DuplicateMappingException)3 MappingException (org.hibernate.MappingException)2 Namespace (org.hibernate.boot.model.relational.Namespace)2 MappingException (org.hibernate.boot.MappingException)1 ImplicitEntityNameSource (org.hibernate.boot.model.naming.ImplicitEntityNameSource)1 InLineViewSource (org.hibernate.boot.model.source.spi.InLineViewSource)1 SecondaryTableSource (org.hibernate.boot.model.source.spi.SecondaryTableSource)1 TableSource (org.hibernate.boot.model.source.spi.TableSource)1 EntityTableXref (org.hibernate.boot.spi.InFlightMetadataCollector.EntityTableXref)1