use of org.hibernate.boot.model.source.spi.AttributeSource 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.source.spi.AttributeSource in project hibernate-orm by hibernate.
the class ModelBinder method bindAllCompositeAttributes.
private void bindAllCompositeAttributes(MappingDocument sourceDocument, EmbeddableSource embeddableSource, Component component) {
for (AttributeSource attributeSource : embeddableSource.attributeSources()) {
Property attribute = null;
if (SingularAttributeSourceBasic.class.isInstance(attributeSource)) {
attribute = createBasicAttribute(sourceDocument, (SingularAttributeSourceBasic) attributeSource, new SimpleValue(sourceDocument.getMetadataCollector(), component.getTable()), component.getComponentClassName());
} else if (SingularAttributeSourceEmbedded.class.isInstance(attributeSource)) {
attribute = createEmbeddedAttribute(sourceDocument, (SingularAttributeSourceEmbedded) attributeSource, new Component(sourceDocument.getMetadataCollector(), component), component.getComponentClassName());
} else if (SingularAttributeSourceManyToOne.class.isInstance(attributeSource)) {
attribute = createManyToOneAttribute(sourceDocument, (SingularAttributeSourceManyToOne) attributeSource, new ManyToOne(sourceDocument.getMetadataCollector(), component.getTable()), component.getComponentClassName());
} else if (SingularAttributeSourceOneToOne.class.isInstance(attributeSource)) {
attribute = createOneToOneAttribute(sourceDocument, (SingularAttributeSourceOneToOne) attributeSource, new OneToOne(sourceDocument.getMetadataCollector(), component.getTable(), component.getOwner()), component.getComponentClassName());
} else if (SingularAttributeSourceAny.class.isInstance(attributeSource)) {
attribute = createAnyAssociationAttribute(sourceDocument, (SingularAttributeSourceAny) attributeSource, new Any(sourceDocument.getMetadataCollector(), component.getTable()), component.getComponentClassName());
} else if (PluralAttributeSource.class.isInstance(attributeSource)) {
attribute = createPluralAttribute(sourceDocument, (PluralAttributeSource) attributeSource, component.getOwner());
} else {
throw new AssertionFailure(String.format(Locale.ENGLISH, "Unexpected AttributeSource sub-type [%s] as part of composite [%s]", attributeSource.getClass().getName(), attributeSource.getAttributeRole().getFullPath()));
}
component.addProperty(attribute);
}
}
use of org.hibernate.boot.model.source.spi.AttributeSource in project hibernate-orm by hibernate.
the class ModelBinder method determineTable.
private Identifier determineTable(MappingDocument mappingDocument, SingularAttributeSourceEmbedded embeddedAttributeSource) {
Identifier tableName = null;
for (AttributeSource attributeSource : embeddedAttributeSource.getEmbeddableSource().attributeSources()) {
final Identifier determinedName;
if (RelationalValueSourceContainer.class.isInstance(attributeSource)) {
determinedName = determineTable(mappingDocument, embeddedAttributeSource.getAttributeRole().getFullPath(), (RelationalValueSourceContainer) attributeSource);
} else if (SingularAttributeSourceEmbedded.class.isInstance(attributeSource)) {
determinedName = determineTable(mappingDocument, (SingularAttributeSourceEmbedded) attributeSource);
} else if (SingularAttributeSourceAny.class.isInstance(attributeSource)) {
determinedName = determineTable(mappingDocument, attributeSource.getAttributeRole().getFullPath(), ((SingularAttributeSourceAny) attributeSource).getKeySource().getRelationalValueSources());
} else {
continue;
}
if (EqualsHelper.equals(tableName, determinedName)) {
continue;
}
if (tableName != null) {
throw new MappingException(String.format(Locale.ENGLISH, "Attribute [%s] referenced columns from multiple tables: %s, %s", embeddedAttributeSource.getAttributeRole().getFullPath(), tableName, determinedName), mappingDocument.getOrigin());
}
tableName = determinedName;
}
return tableName;
}
use of org.hibernate.boot.model.source.spi.AttributeSource 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