use of org.hibernate.boot.model.naming.Identifier 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.getMetadataCollector(), 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.getMetadataCollector(), 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.getMetadataCollector(), 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.getMetadataCollector(), 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.getMetadataCollector(), 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.naming.Identifier in project hibernate-orm by hibernate.
the class ModelBinder method bindBasicEntityValues.
private void bindBasicEntityValues(MappingDocument sourceDocument, AbstractEntitySourceImpl entitySource, PersistentClass entityDescriptor) {
entityDescriptor.setEntityName(entitySource.getEntityNamingSource().getEntityName());
entityDescriptor.setJpaEntityName(entitySource.getEntityNamingSource().getJpaEntityName());
entityDescriptor.setClassName(entitySource.getEntityNamingSource().getClassName());
entityDescriptor.setDiscriminatorValue(entitySource.getDiscriminatorMatchValue() != null ? entitySource.getDiscriminatorMatchValue() : entityDescriptor.getEntityName());
// NOTE : entitySource#isLazy already accounts for MappingDefaults#areEntitiesImplicitlyLazy
if (StringHelper.isNotEmpty(entitySource.getProxy())) {
final String qualifiedProxyName = sourceDocument.qualifyClassName(entitySource.getProxy());
entityDescriptor.setProxyInterfaceName(qualifiedProxyName);
entityDescriptor.setLazy(true);
} else if (entitySource.isLazy()) {
entityDescriptor.setProxyInterfaceName(entityDescriptor.getClassName());
entityDescriptor.setLazy(true);
} else {
entityDescriptor.setProxyInterfaceName(null);
entityDescriptor.setLazy(false);
}
entityDescriptor.setAbstract(entitySource.isAbstract());
sourceDocument.getMetadataCollector().addImport(entitySource.getEntityNamingSource().getEntityName(), entitySource.getEntityNamingSource().getEntityName());
if (sourceDocument.getMappingDefaults().isAutoImportEnabled() && entitySource.getEntityNamingSource().getEntityName().indexOf('.') > 0) {
sourceDocument.getMetadataCollector().addImport(StringHelper.unqualify(entitySource.getEntityNamingSource().getEntityName()), entitySource.getEntityNamingSource().getEntityName());
}
if (entitySource.getTuplizerClassMap() != null) {
if (entitySource.getTuplizerClassMap().size() > 1) {
DeprecationLogger.DEPRECATION_LOGGER.logDeprecationOfMultipleEntityModeSupport();
}
for (Map.Entry<EntityMode, String> tuplizerEntry : entitySource.getTuplizerClassMap().entrySet()) {
entityDescriptor.addTuplizer(tuplizerEntry.getKey(), tuplizerEntry.getValue());
}
}
if (StringHelper.isNotEmpty(entitySource.getXmlNodeName())) {
DeprecationLogger.DEPRECATION_LOGGER.logDeprecationOfDomEntityModeSupport();
}
entityDescriptor.setDynamicInsert(entitySource.isDynamicInsert());
entityDescriptor.setDynamicUpdate(entitySource.isDynamicUpdate());
entityDescriptor.setBatchSize(entitySource.getBatchSize());
entityDescriptor.setSelectBeforeUpdate(entitySource.isSelectBeforeUpdate());
if (StringHelper.isNotEmpty(entitySource.getCustomPersisterClassName())) {
try {
entityDescriptor.setEntityPersisterClass(sourceDocument.getClassLoaderAccess().classForName(entitySource.getCustomPersisterClassName()));
} catch (ClassLoadingException e) {
throw new MappingException(String.format(Locale.ENGLISH, "Unable to load specified persister class : %s", entitySource.getCustomPersisterClassName()), e, sourceDocument.getOrigin());
}
}
bindCustomSql(sourceDocument, entitySource, entityDescriptor);
final JdbcEnvironment jdbcEnvironment = sourceDocument.getMetadataCollector().getDatabase().getJdbcEnvironment();
for (String tableName : entitySource.getSynchronizedTableNames()) {
final Identifier physicalTableName = sourceDocument.getBuildingOptions().getPhysicalNamingStrategy().toPhysicalTableName(jdbcEnvironment.getIdentifierHelper().toIdentifier(tableName), jdbcEnvironment);
entityDescriptor.addSynchronizedTable(physicalTableName.render(jdbcEnvironment.getDialect()));
}
for (FilterSource filterSource : entitySource.getFilterSources()) {
String condition = filterSource.getCondition();
if (condition == null) {
final FilterDefinition filterDefinition = sourceDocument.getMetadataCollector().getFilterDefinition(filterSource.getName());
if (filterDefinition != null) {
condition = filterDefinition.getDefaultFilterCondition();
}
}
entityDescriptor.addFilter(filterSource.getName(), condition, filterSource.shouldAutoInjectAliases(), filterSource.getAliasToTableMap(), filterSource.getAliasToEntityMap());
}
for (JaxbHbmNamedQueryType namedQuery : entitySource.getNamedQueries()) {
NamedQueryBinder.processNamedQuery(sourceDocument, namedQuery, entitySource.getEntityNamingSource().getEntityName() + ".");
}
for (JaxbHbmNamedNativeQueryType namedQuery : entitySource.getNamedNativeQueries()) {
NamedQueryBinder.processNamedNativeQuery(sourceDocument, namedQuery, entitySource.getEntityNamingSource().getEntityName() + ".");
}
entityDescriptor.setMetaAttributes(entitySource.getToolingHintContext().getMetaAttributeMap());
}
use of org.hibernate.boot.model.naming.Identifier 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.naming.Identifier 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.getMetadataCollector(), 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.naming.Identifier 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;
}
Aggregations