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();
}
}
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());
}
}
}
}
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;
}
Aggregations