use of org.hibernate.mapping.Property in project hibernate-orm by hibernate.
the class TestTools method extractModProperties.
public static Set<String> extractModProperties(PersistentClass persistentClass, String suffix) {
final Set<String> result = new HashSet<String>();
final Iterator iterator = persistentClass.getPropertyIterator();
while (iterator.hasNext()) {
final Property property = (Property) iterator.next();
final String propertyName = property.getName();
if (propertyName.endsWith(suffix)) {
result.add(propertyName);
}
}
return result;
}
use of org.hibernate.mapping.Property 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.mapping.Property in project hibernate-orm by hibernate.
the class ModelBinder method bindNonAggregatedCompositeEntityIdentifier.
private void bindNonAggregatedCompositeEntityIdentifier(MappingDocument mappingDocument, EntityHierarchySourceImpl hierarchySource, RootClass rootEntityDescriptor) {
final IdentifierSourceNonAggregatedComposite identifierSource = (IdentifierSourceNonAggregatedComposite) hierarchySource.getIdentifierSource();
final Component cid = new Component(mappingDocument.getMetadataCollector(), rootEntityDescriptor);
cid.setKey(true);
rootEntityDescriptor.setIdentifier(cid);
final String idClassName = extractIdClassName(identifierSource);
bindComponent(mappingDocument, hierarchySource.getRoot().getAttributeRoleBase().append("<id>").getFullPath(), identifierSource.getEmbeddableSource(), cid, idClassName, rootEntityDescriptor.getClassName(), null, idClassName == null, false, null);
if (idClassName != null) {
// we also need to bind the "id mapper". ugh, terrible name. Basically we need to
// create a virtual (embedded) composite for the non-aggregated attributes on the entity
// itself.
final Component mapper = new Component(mappingDocument.getMetadataCollector(), rootEntityDescriptor);
bindComponent(mappingDocument, hierarchySource.getRoot().getAttributeRoleBase().append(ID_MAPPER_PATH_PART).getFullPath(), identifierSource.getEmbeddableSource(), mapper, rootEntityDescriptor.getClassName(), null, null, true, false, null);
rootEntityDescriptor.setIdentifierMapper(mapper);
Property property = new Property();
property.setName(PropertyPath.IDENTIFIER_MAPPER_PROPERTY);
property.setUpdateable(false);
property.setInsertable(false);
property.setValue(mapper);
property.setPropertyAccessorName("embedded");
rootEntityDescriptor.addProperty(property);
}
finishBindingCompositeIdentifier(mappingDocument, rootEntityDescriptor, identifierSource, cid, null);
}
use of org.hibernate.mapping.Property in project hibernate-orm by hibernate.
the class ModelBinder method finishBindingCompositeIdentifier.
private void finishBindingCompositeIdentifier(MappingDocument sourceDocument, RootClass rootEntityDescriptor, CompositeIdentifierSource identifierSource, Component cid, String propertyName) {
if (propertyName == null) {
rootEntityDescriptor.setEmbeddedIdentifier(cid.isEmbedded());
if (cid.isEmbedded()) {
// todo : what is the implication of this?
cid.setDynamic(!rootEntityDescriptor.hasPojoRepresentation());
/*
* Property prop = new Property(); prop.setName("id");
* prop.setPropertyAccessorName("embedded"); prop.setValue(id);
* entity.setIdentifierProperty(prop);
*/
}
} else {
Property prop = new Property();
prop.setValue(cid);
bindProperty(sourceDocument, ((IdentifierSourceAggregatedComposite) identifierSource).getIdentifierAttributeSource(), prop);
rootEntityDescriptor.setIdentifierProperty(prop);
rootEntityDescriptor.setDeclaredIdentifierProperty(prop);
}
makeIdentifier(sourceDocument, identifierSource.getIdentifierGeneratorDescriptor(), null, cid);
}
use of org.hibernate.mapping.Property 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);
}
Aggregations