use of org.hibernate.boot.model.source.spi.LocalMetadataBuildingContext in project hibernate-orm by hibernate.
the class ModelBinder method bindEntityDiscriminator.
private void bindEntityDiscriminator(MappingDocument sourceDocument, final EntityHierarchySourceImpl hierarchySource, RootClass rootEntityDescriptor) {
final SimpleValue discriminatorValue = new SimpleValue(sourceDocument, rootEntityDescriptor.getTable());
rootEntityDescriptor.setDiscriminator(discriminatorValue);
String typeName = hierarchySource.getDiscriminatorSource().getExplicitHibernateTypeName();
if (typeName == null) {
typeName = "string";
}
bindSimpleValueType(sourceDocument, new HibernateTypeSourceImpl(typeName), discriminatorValue);
relationalObjectBinder.bindColumnOrFormula(sourceDocument, hierarchySource.getDiscriminatorSource().getDiscriminatorRelationalValueSource(), discriminatorValue, false, new RelationalObjectBinder.ColumnNamingDelegate() {
@Override
public Identifier determineImplicitName(final LocalMetadataBuildingContext context) {
return implicitNamingStrategy.determineDiscriminatorColumnName(hierarchySource.getDiscriminatorSource());
}
});
rootEntityDescriptor.setPolymorphic(true);
rootEntityDescriptor.setDiscriminatorInsertable(hierarchySource.getDiscriminatorSource().isInserted());
// todo : currently isForced() is defined as boolean, not Boolean
// although it has always been that way (DTD too)
final boolean force = hierarchySource.getDiscriminatorSource().isForced() || sourceDocument.getBuildingOptions().shouldImplicitlyForceDiscriminatorInSelect();
rootEntityDescriptor.setForceDiscriminator(force);
}
use of org.hibernate.boot.model.source.spi.LocalMetadataBuildingContext in project hibernate-orm by hibernate.
the class ModelBinder method bindListOrArrayIndex.
public void bindListOrArrayIndex(MappingDocument mappingDocument, final IndexedPluralAttributeSource attributeSource, org.hibernate.mapping.List collectionBinding) {
final PluralAttributeSequentialIndexSource indexSource = (PluralAttributeSequentialIndexSource) attributeSource.getIndexSource();
final SimpleValue indexBinding = new SimpleValue(mappingDocument, collectionBinding.getCollectionTable());
bindSimpleValueType(mappingDocument, indexSource.getTypeInformation(), indexBinding);
relationalObjectBinder.bindColumnsAndFormulas(mappingDocument, indexSource.getRelationalValueSources(), indexBinding, attributeSource.getElementSource() instanceof PluralAttributeElementSourceOneToMany, new RelationalObjectBinder.ColumnNamingDelegate() {
@Override
public Identifier determineImplicitName(final LocalMetadataBuildingContext context) {
return context.getBuildingOptions().getImplicitNamingStrategy().determineListIndexColumnName(new ImplicitIndexColumnNameSource() {
@Override
public AttributePath getPluralAttributePath() {
return attributeSource.getAttributePath();
}
@Override
public MetadataBuildingContext getBuildingContext() {
return context;
}
});
}
});
collectionBinding.setIndex(indexBinding);
collectionBinding.setBaseIndex(indexSource.getBase());
}
use of org.hibernate.boot.model.source.spi.LocalMetadataBuildingContext in project hibernate-orm by hibernate.
the class ModelBinder method bindSimpleEntityIdentifier.
private void bindSimpleEntityIdentifier(MappingDocument sourceDocument, final EntityHierarchySourceImpl hierarchySource, RootClass rootEntityDescriptor) {
final IdentifierSourceSimple idSource = (IdentifierSourceSimple) hierarchySource.getIdentifierSource();
final SimpleValue idValue = new SimpleValue(sourceDocument, rootEntityDescriptor.getTable());
rootEntityDescriptor.setIdentifier(idValue);
bindSimpleValueType(sourceDocument, idSource.getIdentifierAttributeSource().getTypeInformation(), idValue);
final String propertyName = idSource.getIdentifierAttributeSource().getName();
if (propertyName == null || !rootEntityDescriptor.hasPojoRepresentation()) {
if (!idValue.isTypeSpecified()) {
throw new MappingException("must specify an identifier type: " + rootEntityDescriptor.getEntityName(), sourceDocument.getOrigin());
}
} else {
idValue.setTypeUsingReflection(rootEntityDescriptor.getClassName(), propertyName);
}
relationalObjectBinder.bindColumnsAndFormulas(sourceDocument, ((RelationalValueSourceContainer) idSource.getIdentifierAttributeSource()).getRelationalValueSources(), idValue, false, new RelationalObjectBinder.ColumnNamingDelegate() {
@Override
public Identifier determineImplicitName(final LocalMetadataBuildingContext context) {
context.getBuildingOptions().getImplicitNamingStrategy().determineIdentifierColumnName(new ImplicitIdentifierColumnNameSource() {
@Override
public EntityNaming getEntityNaming() {
return hierarchySource.getRoot().getEntityNamingSource();
}
@Override
public AttributePath getIdentifierAttributePath() {
return idSource.getIdentifierAttributeSource().getAttributePath();
}
@Override
public MetadataBuildingContext getBuildingContext() {
return context;
}
});
return database.toIdentifier(propertyName);
}
});
if (propertyName != null) {
Property prop = new Property();
prop.setValue(idValue);
bindProperty(sourceDocument, idSource.getIdentifierAttributeSource(), prop);
rootEntityDescriptor.setIdentifierProperty(prop);
rootEntityDescriptor.setDeclaredIdentifierProperty(prop);
}
makeIdentifier(sourceDocument, idSource.getIdentifierGeneratorDescriptor(), idSource.getUnsavedValue(), idValue);
}
use of org.hibernate.boot.model.source.spi.LocalMetadataBuildingContext in project hibernate-orm by hibernate.
the class ModelBinder method bindAny.
private void bindAny(MappingDocument sourceDocument, final AnyMappingSource anyMapping, Any anyBinding, final AttributeRole attributeRole, AttributePath attributePath) {
final TypeResolution keyTypeResolution = resolveType(sourceDocument, anyMapping.getKeySource().getTypeSource());
if (keyTypeResolution != null) {
anyBinding.setIdentifierType(keyTypeResolution.typeName);
}
final TypeResolution discriminatorTypeResolution = resolveType(sourceDocument, anyMapping.getDiscriminatorSource().getTypeSource());
if (discriminatorTypeResolution != null) {
anyBinding.setMetaType(discriminatorTypeResolution.typeName);
try {
final DiscriminatorType metaType = (DiscriminatorType) sourceDocument.getMetadataCollector().getTypeResolver().heuristicType(discriminatorTypeResolution.typeName);
final HashMap anyValueBindingMap = new HashMap();
for (Map.Entry<String, String> discriminatorValueMappings : anyMapping.getDiscriminatorSource().getValueMappings().entrySet()) {
try {
final Object discriminatorValue = metaType.stringToObject(discriminatorValueMappings.getKey());
final String mappedEntityName = sourceDocument.qualifyClassName(discriminatorValueMappings.getValue());
// noinspection unchecked
anyValueBindingMap.put(discriminatorValue, mappedEntityName);
} catch (Exception e) {
throw new MappingException(String.format(Locale.ENGLISH, "Unable to interpret <meta-value value=\"%s\" class=\"%s\"/> defined as part of <any/> attribute [%s]", discriminatorValueMappings.getKey(), discriminatorValueMappings.getValue(), attributeRole.getFullPath()), e, sourceDocument.getOrigin());
}
}
anyBinding.setMetaValues(anyValueBindingMap);
} catch (ClassCastException e) {
throw new MappingException(String.format(Locale.ENGLISH, "Specified meta-type [%s] for <any/> attribute [%s] did not implement DiscriminatorType", discriminatorTypeResolution.typeName, attributeRole.getFullPath()), e, sourceDocument.getOrigin());
}
}
relationalObjectBinder.bindColumnOrFormula(sourceDocument, anyMapping.getDiscriminatorSource().getRelationalValueSource(), anyBinding, true, new RelationalObjectBinder.ColumnNamingDelegate() {
@Override
public Identifier determineImplicitName(LocalMetadataBuildingContext context) {
return implicitNamingStrategy.determineAnyDiscriminatorColumnName(anyMapping.getDiscriminatorSource());
}
});
relationalObjectBinder.bindColumnsAndFormulas(sourceDocument, anyMapping.getKeySource().getRelationalValueSources(), anyBinding, true, new RelationalObjectBinder.ColumnNamingDelegate() {
@Override
public Identifier determineImplicitName(LocalMetadataBuildingContext context) {
return implicitNamingStrategy.determineAnyKeyColumnName(anyMapping.getKeySource());
}
});
}
use of org.hibernate.boot.model.source.spi.LocalMetadataBuildingContext 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();
}
}
Aggregations