Search in sources :

Example 1 with TableSource

use of org.hibernate.boot.model.source.spi.TableSource 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 TableSource

use of org.hibernate.boot.model.source.spi.TableSource in project hibernate-orm by hibernate.

the class ModelBinder method bindSecondaryTable.

private void bindSecondaryTable(MappingDocument mappingDocument, SecondaryTableSource secondaryTableSource, Join secondaryTableJoin, final EntityTableXref entityTableXref) {
    final PersistentClass persistentClass = secondaryTableJoin.getPersistentClass();
    final Identifier catalogName = determineCatalogName(secondaryTableSource.getTableSource());
    final Identifier schemaName = determineSchemaName(secondaryTableSource.getTableSource());
    final Namespace namespace = database.locateNamespace(catalogName, schemaName);
    Table secondaryTable;
    final Identifier logicalTableName;
    if (TableSource.class.isInstance(secondaryTableSource.getTableSource())) {
        final TableSource tableSource = (TableSource) secondaryTableSource.getTableSource();
        logicalTableName = database.toIdentifier(tableSource.getExplicitTableName());
        secondaryTable = namespace.locateTable(logicalTableName);
        if (secondaryTable == null) {
            secondaryTable = namespace.createTable(logicalTableName, false);
        } else {
            secondaryTable.setAbstract(false);
        }
        secondaryTable.setComment(tableSource.getComment());
    } else {
        final InLineViewSource inLineViewSource = (InLineViewSource) secondaryTableSource.getTableSource();
        secondaryTable = new Table(namespace, inLineViewSource.getSelectStatement(), false);
        logicalTableName = Identifier.toIdentifier(inLineViewSource.getLogicalName());
    }
    secondaryTableJoin.setTable(secondaryTable);
    entityTableXref.addSecondaryTable(mappingDocument, logicalTableName, secondaryTableJoin);
    bindCustomSql(mappingDocument, secondaryTableSource, secondaryTableJoin);
    secondaryTableJoin.setSequentialSelect(secondaryTableSource.getFetchStyle() == FetchStyle.SELECT);
    secondaryTableJoin.setInverse(secondaryTableSource.isInverse());
    secondaryTableJoin.setOptional(secondaryTableSource.isOptional());
    if (log.isDebugEnabled()) {
        log.debugf("Mapping entity secondary-table: %s -> %s", persistentClass.getEntityName(), secondaryTable.getName());
    }
    final SimpleValue keyBinding = new DependantValue(mappingDocument, secondaryTable, persistentClass.getIdentifier());
    if (mappingDocument.getBuildingOptions().useNationalizedCharacterData()) {
        keyBinding.makeNationalized();
    }
    secondaryTableJoin.setKey(keyBinding);
    keyBinding.setCascadeDeleteEnabled(secondaryTableSource.isCascadeDeleteEnabled());
    // NOTE : no Type info to bind...
    relationalObjectBinder.bindColumns(mappingDocument, secondaryTableSource.getPrimaryKeyColumnSources(), keyBinding, secondaryTableSource.isOptional(), new RelationalObjectBinder.ColumnNamingDelegate() {

        int count = 0;

        @Override
        public Identifier determineImplicitName(LocalMetadataBuildingContext context) {
            final Column correspondingColumn = entityTableXref.getPrimaryTable().getPrimaryKey().getColumn(count++);
            return database.toIdentifier(correspondingColumn.getQuotedName());
        }
    });
    keyBinding.setForeignKeyName(secondaryTableSource.getExplicitForeignKeyName());
    // skip creating primary and foreign keys for a subselect.
    if (secondaryTable.getSubselect() == null) {
        secondaryTableJoin.createPrimaryKey();
        secondaryTableJoin.createForeignKey();
    }
}
Also used : Table(org.hibernate.mapping.Table) DenormalizedTable(org.hibernate.mapping.DenormalizedTable) DependantValue(org.hibernate.mapping.DependantValue) InLineViewSource(org.hibernate.boot.model.source.spi.InLineViewSource) Namespace(org.hibernate.boot.model.relational.Namespace) SimpleValue(org.hibernate.mapping.SimpleValue) Identifier(org.hibernate.boot.model.naming.Identifier) TableSource(org.hibernate.boot.model.source.spi.TableSource) SecondaryTableSource(org.hibernate.boot.model.source.spi.SecondaryTableSource) Column(org.hibernate.mapping.Column) LocalMetadataBuildingContext(org.hibernate.boot.model.source.spi.LocalMetadataBuildingContext) PersistentClass(org.hibernate.mapping.PersistentClass)

Aggregations

Identifier (org.hibernate.boot.model.naming.Identifier)2 Namespace (org.hibernate.boot.model.relational.Namespace)2 InLineViewSource (org.hibernate.boot.model.source.spi.InLineViewSource)2 SecondaryTableSource (org.hibernate.boot.model.source.spi.SecondaryTableSource)2 TableSource (org.hibernate.boot.model.source.spi.TableSource)2 DenormalizedTable (org.hibernate.mapping.DenormalizedTable)2 Table (org.hibernate.mapping.Table)2 MappingException (org.hibernate.boot.MappingException)1 ImplicitEntityNameSource (org.hibernate.boot.model.naming.ImplicitEntityNameSource)1 LocalMetadataBuildingContext (org.hibernate.boot.model.source.spi.LocalMetadataBuildingContext)1 EntityTableXref (org.hibernate.boot.spi.InFlightMetadataCollector.EntityTableXref)1 Column (org.hibernate.mapping.Column)1 DependantValue (org.hibernate.mapping.DependantValue)1 PersistentClass (org.hibernate.mapping.PersistentClass)1 SimpleValue (org.hibernate.mapping.SimpleValue)1