Search in sources :

Example 1 with SecondaryTableSource

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

Example 2 with SecondaryTableSource

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

the class ModelBinder method bindAllEntityAttributes.

private void bindAllEntityAttributes(MappingDocument mappingDocument, EntitySource entitySource, PersistentClass entityDescriptor) {
    final EntityTableXref entityTableXref = mappingDocument.getMetadataCollector().getEntityTableXref(entityDescriptor.getEntityName());
    if (entityTableXref == null) {
        throw new AssertionFailure(String.format(Locale.ENGLISH, "Unable to locate EntityTableXref for entity [%s] : %s", entityDescriptor.getEntityName(), mappingDocument.getOrigin()));
    }
    // make sure we bind secondary tables first!
    for (SecondaryTableSource secondaryTableSource : entitySource.getSecondaryTableMap().values()) {
        final Join secondaryTableJoin = new Join();
        secondaryTableJoin.setPersistentClass(entityDescriptor);
        bindSecondaryTable(mappingDocument, secondaryTableSource, secondaryTableJoin, entityTableXref);
        entityDescriptor.addJoin(secondaryTableJoin);
    }
    for (AttributeSource attributeSource : entitySource.attributeSources()) {
        if (PluralAttributeSource.class.isInstance(attributeSource)) {
            // plural attribute
            final Property attribute = createPluralAttribute(mappingDocument, (PluralAttributeSource) attributeSource, entityDescriptor);
            entityDescriptor.addProperty(attribute);
        } else {
            // singular attribute
            if (SingularAttributeSourceBasic.class.isInstance(attributeSource)) {
                final SingularAttributeSourceBasic basicAttributeSource = (SingularAttributeSourceBasic) attributeSource;
                final Identifier tableName = determineTable(mappingDocument, basicAttributeSource.getName(), basicAttributeSource);
                final AttributeContainer attributeContainer;
                final Table table;
                final Join secondaryTableJoin = entityTableXref.locateJoin(tableName);
                if (secondaryTableJoin == null) {
                    table = entityDescriptor.getTable();
                    attributeContainer = entityDescriptor;
                } else {
                    table = secondaryTableJoin.getTable();
                    attributeContainer = secondaryTableJoin;
                }
                final Property attribute = createBasicAttribute(mappingDocument, basicAttributeSource, new SimpleValue(mappingDocument, table), entityDescriptor.getClassName());
                if (secondaryTableJoin != null) {
                    attribute.setOptional(secondaryTableJoin.isOptional());
                }
                attributeContainer.addProperty(attribute);
                handleNaturalIdBinding(mappingDocument, entityDescriptor, attribute, basicAttributeSource.getNaturalIdMutability());
            } else if (SingularAttributeSourceEmbedded.class.isInstance(attributeSource)) {
                final SingularAttributeSourceEmbedded embeddedAttributeSource = (SingularAttributeSourceEmbedded) attributeSource;
                final Identifier tableName = determineTable(mappingDocument, embeddedAttributeSource);
                final AttributeContainer attributeContainer;
                final Table table;
                final Join secondaryTableJoin = entityTableXref.locateJoin(tableName);
                if (secondaryTableJoin == null) {
                    table = entityDescriptor.getTable();
                    attributeContainer = entityDescriptor;
                } else {
                    table = secondaryTableJoin.getTable();
                    attributeContainer = secondaryTableJoin;
                }
                final Property attribute = createEmbeddedAttribute(mappingDocument, (SingularAttributeSourceEmbedded) attributeSource, new Component(mappingDocument, table, entityDescriptor), entityDescriptor.getClassName());
                if (secondaryTableJoin != null) {
                    attribute.setOptional(secondaryTableJoin.isOptional());
                }
                attributeContainer.addProperty(attribute);
                handleNaturalIdBinding(mappingDocument, entityDescriptor, attribute, embeddedAttributeSource.getNaturalIdMutability());
            } else if (SingularAttributeSourceManyToOne.class.isInstance(attributeSource)) {
                final SingularAttributeSourceManyToOne manyToOneAttributeSource = (SingularAttributeSourceManyToOne) attributeSource;
                final Identifier tableName = determineTable(mappingDocument, manyToOneAttributeSource.getName(), manyToOneAttributeSource);
                final AttributeContainer attributeContainer;
                final Table table;
                final Join secondaryTableJoin = entityTableXref.locateJoin(tableName);
                if (secondaryTableJoin == null) {
                    table = entityDescriptor.getTable();
                    attributeContainer = entityDescriptor;
                } else {
                    table = secondaryTableJoin.getTable();
                    attributeContainer = secondaryTableJoin;
                }
                final Property attribute = createManyToOneAttribute(mappingDocument, manyToOneAttributeSource, new ManyToOne(mappingDocument, table), entityDescriptor.getClassName());
                if (secondaryTableJoin != null) {
                    attribute.setOptional(secondaryTableJoin.isOptional());
                }
                attributeContainer.addProperty(attribute);
                handleNaturalIdBinding(mappingDocument, entityDescriptor, attribute, manyToOneAttributeSource.getNaturalIdMutability());
            } else if (SingularAttributeSourceOneToOne.class.isInstance(attributeSource)) {
                final SingularAttributeSourceOneToOne oneToOneAttributeSource = (SingularAttributeSourceOneToOne) attributeSource;
                final Table table = entityDescriptor.getTable();
                final Property attribute = createOneToOneAttribute(mappingDocument, oneToOneAttributeSource, new OneToOne(mappingDocument, table, entityDescriptor), entityDescriptor.getClassName());
                entityDescriptor.addProperty(attribute);
                handleNaturalIdBinding(mappingDocument, entityDescriptor, attribute, oneToOneAttributeSource.getNaturalIdMutability());
            } else if (SingularAttributeSourceAny.class.isInstance(attributeSource)) {
                final SingularAttributeSourceAny anyAttributeSource = (SingularAttributeSourceAny) attributeSource;
                final Identifier tableName = determineTable(mappingDocument, anyAttributeSource.getName(), anyAttributeSource.getKeySource().getRelationalValueSources());
                final AttributeContainer attributeContainer;
                final Table table;
                final Join secondaryTableJoin = entityTableXref.locateJoin(tableName);
                if (secondaryTableJoin == null) {
                    table = entityDescriptor.getTable();
                    attributeContainer = entityDescriptor;
                } else {
                    table = secondaryTableJoin.getTable();
                    attributeContainer = secondaryTableJoin;
                }
                final Property attribute = createAnyAssociationAttribute(mappingDocument, anyAttributeSource, new Any(mappingDocument, table), entityDescriptor.getEntityName());
                if (secondaryTableJoin != null) {
                    attribute.setOptional(secondaryTableJoin.isOptional());
                }
                attributeContainer.addProperty(attribute);
                handleNaturalIdBinding(mappingDocument, entityDescriptor, attribute, anyAttributeSource.getNaturalIdMutability());
            }
        }
    }
}
Also used : SingularAttributeSourceBasic(org.hibernate.boot.model.source.spi.SingularAttributeSourceBasic) AssertionFailure(org.hibernate.AssertionFailure) VersionAttributeSource(org.hibernate.boot.model.source.spi.VersionAttributeSource) AttributeSource(org.hibernate.boot.model.source.spi.AttributeSource) PluralAttributeSource(org.hibernate.boot.model.source.spi.PluralAttributeSource) SingularAttributeSource(org.hibernate.boot.model.source.spi.SingularAttributeSource) Table(org.hibernate.mapping.Table) DenormalizedTable(org.hibernate.mapping.DenormalizedTable) SingularAttributeSourceAny(org.hibernate.boot.model.source.spi.SingularAttributeSourceAny) Join(org.hibernate.mapping.Join) SingularAttributeSourceManyToOne(org.hibernate.boot.model.source.spi.SingularAttributeSourceManyToOne) AttributeContainer(org.hibernate.mapping.AttributeContainer) SecondaryTableSource(org.hibernate.boot.model.source.spi.SecondaryTableSource) SingularAttributeSourceOneToOne(org.hibernate.boot.model.source.spi.SingularAttributeSourceOneToOne) PluralAttributeElementSourceManyToAny(org.hibernate.boot.model.source.spi.PluralAttributeElementSourceManyToAny) Any(org.hibernate.mapping.Any) SingularAttributeSourceAny(org.hibernate.boot.model.source.spi.SingularAttributeSourceAny) SingularAttributeSourceEmbedded(org.hibernate.boot.model.source.spi.SingularAttributeSourceEmbedded) SingularAttributeSourceManyToOne(org.hibernate.boot.model.source.spi.SingularAttributeSourceManyToOne) ManyToOne(org.hibernate.mapping.ManyToOne) SimpleValue(org.hibernate.mapping.SimpleValue) OneToOne(org.hibernate.mapping.OneToOne) SingularAttributeSourceOneToOne(org.hibernate.boot.model.source.spi.SingularAttributeSourceOneToOne) Identifier(org.hibernate.boot.model.naming.Identifier) EntityTableXref(org.hibernate.boot.spi.InFlightMetadataCollector.EntityTableXref) Component(org.hibernate.mapping.Component) Property(org.hibernate.mapping.Property) SyntheticProperty(org.hibernate.mapping.SyntheticProperty)

Example 3 with SecondaryTableSource

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

the class AbstractEntitySourceImpl method buildSecondaryTableMap.

private Map<String, SecondaryTableSource> buildSecondaryTableMap() {
    if (!SecondaryTableContainer.class.isInstance(jaxbEntityMapping)) {
        return Collections.emptyMap();
    }
    final HashMap<String, SecondaryTableSource> secondaryTableSourcesMap = new HashMap<String, SecondaryTableSource>();
    for (final JaxbHbmSecondaryTableType joinElement : ((SecondaryTableContainer) jaxbEntityMapping).getJoin()) {
        final SecondaryTableSourceImpl secondaryTableSource = new SecondaryTableSourceImpl(sourceMappingDocument(), joinElement, getEntityNamingSource(), this);
        final String logicalTableName = secondaryTableSource.getLogicalTableNameForContainedColumns();
        secondaryTableSourcesMap.put(logicalTableName, secondaryTableSource);
        AttributesHelper.processAttributes(sourceMappingDocument(), new AttributesHelper.Callback() {

            @Override
            public AttributeSourceContainer getAttributeSourceContainer() {
                return AbstractEntitySourceImpl.this;
            }

            @Override
            public void addAttributeSource(AttributeSource attributeSource) {
                attributeSources.add(attributeSource);
            }
        }, joinElement.getAttributes(), logicalTableName, NaturalIdMutability.NOT_NATURAL_ID);
    }
    return secondaryTableSourcesMap;
}
Also used : AttributeSourceContainer(org.hibernate.boot.model.source.spi.AttributeSourceContainer) JaxbHbmSecondaryTableType(org.hibernate.boot.jaxb.hbm.spi.JaxbHbmSecondaryTableType) AttributeSource(org.hibernate.boot.model.source.spi.AttributeSource) HashMap(java.util.HashMap) SecondaryTableSource(org.hibernate.boot.model.source.spi.SecondaryTableSource) SecondaryTableContainer(org.hibernate.boot.jaxb.hbm.spi.SecondaryTableContainer)

Aggregations

SecondaryTableSource (org.hibernate.boot.model.source.spi.SecondaryTableSource)3 Identifier (org.hibernate.boot.model.naming.Identifier)2 AttributeSource (org.hibernate.boot.model.source.spi.AttributeSource)2 DenormalizedTable (org.hibernate.mapping.DenormalizedTable)2 SimpleValue (org.hibernate.mapping.SimpleValue)2 Table (org.hibernate.mapping.Table)2 HashMap (java.util.HashMap)1 AssertionFailure (org.hibernate.AssertionFailure)1 JaxbHbmSecondaryTableType (org.hibernate.boot.jaxb.hbm.spi.JaxbHbmSecondaryTableType)1 SecondaryTableContainer (org.hibernate.boot.jaxb.hbm.spi.SecondaryTableContainer)1 Namespace (org.hibernate.boot.model.relational.Namespace)1 AttributeSourceContainer (org.hibernate.boot.model.source.spi.AttributeSourceContainer)1 InLineViewSource (org.hibernate.boot.model.source.spi.InLineViewSource)1 LocalMetadataBuildingContext (org.hibernate.boot.model.source.spi.LocalMetadataBuildingContext)1 PluralAttributeElementSourceManyToAny (org.hibernate.boot.model.source.spi.PluralAttributeElementSourceManyToAny)1 PluralAttributeSource (org.hibernate.boot.model.source.spi.PluralAttributeSource)1 SingularAttributeSource (org.hibernate.boot.model.source.spi.SingularAttributeSource)1 SingularAttributeSourceAny (org.hibernate.boot.model.source.spi.SingularAttributeSourceAny)1 SingularAttributeSourceBasic (org.hibernate.boot.model.source.spi.SingularAttributeSourceBasic)1 SingularAttributeSourceEmbedded (org.hibernate.boot.model.source.spi.SingularAttributeSourceEmbedded)1